0

Trying to break up a dataframe into train, val, and test dataframes based on its row index e.g. observation 1 will go into training, 2 into val, and 3 into test, however, I'm hitting a roadblock. Here's my code thus far:

climbingTngDataset = pd.DataFrame([])
climbingValDataset = pd.DataFrame([])
climbingTestDataset = pd.DataFrame([])

for i in range(len(dfClimbing)):
    if i % 2 == 0:
       climbingValDataset.append(i) 
    if i % 3 == 0:
        climbingTestDataset.append(i)
    else:
        climbingTngDataset.append(i)
0

1 Answer 1

1

Use groupby to split your dataFrame:

train, test, val = [
    g for _, g in dfClimbing.groupby(dfClimbing.index % 3)
]

Demo
(With two splits instead of 3)

print(df)
   Record ID Para Tag
0          1    A   x
1          1    A   y
2          2    B   x
3          2    B   y
4          1    A   z

i, j = [g for _, g in df.groupby(df.index % 2)]

print(i)
   Record ID Para Tag
0          1    A   x
2          2    B   x
4          1    A   z

print(j)
   Record ID Para Tag
1          1    A   y
3          2    B   y
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.