Skip to content

Instantly share code, notes, and snippets.

@shaypal5
Created June 27, 2018 08:39
Show Gist options
  • Select an option

  • Save shaypal5/3e34e85bd89d65d4ac118daa9a42b174 to your computer and use it in GitHub Desktop.

Select an option

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

jo3p commented Jan 5, 2021

Copy link
Copy Markdown

Thanks for this!

@cyuab

cyuab commented Feb 18, 2025

Copy link
Copy Markdown

Thanks for the code!

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