Skip to content

Instantly share code, notes, and snippets.

@shaypal5
Created June 27, 2018 08:39
Show Gist options
  • Star 0 You must be signed in to star a gist
  • Fork 0 You must be signed in to fork a gist
  • Save shaypal5/3e34e85bd89d65d4ac118daa9a42b174 to your computer and use it in GitHub Desktop.
Save shaypal5/3e34e85bd89d65d4ac118daa9a42b174 to your computer and use it in GitHub Desktop.
Use sklearn's train_test_split on the first level of a multi-indexed pandas DataFrame
import pandas as pd
from sklearn.model_selection import train_test_split
df = pd.DataFrame(data=[[1], [2], [3]], columns=['foo'], index=pd.MultiIndex.from_tuples(((0,'a'),(0, 'b'), (1, 'a'))))
train_ix, test_ix = train_test_split(df.index.levels[0])
train_df = df.loc[train_ix]
test_df = df.loc[test_ix]
@jo3p
Copy link

jo3p commented Jan 5, 2021

Thanks for this!

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment