1

I have the following script:

 data = {
       'File':[1,1,2,2,3],
       'Sheet':['Sheet 1','Sheet 1','Sheet 3','Sheet3','Sheet5'],
       'Duration':[2,5,3,7,9],
       'Cost':[7,5,8,9,3],
       'Distance':[2,4,5,7,5]
}
 df=pd.DataFrame(data)
 df.columns=[['Set X','Set X','Set Y','Set Y','Set Z'],['File','Sheet','Duration','Cost','Distance']]

I would like to separate this single dataframe into 3 dataframes based on the 'File' type and 'Sheet' type, such that the 3 separate dataframes looks like this: enter image description here

How should I script with the multiindex? Thank you in advance :)

1
  • Note the typo 'Sheet3' vs 'Sheet 3'. Commented Jul 7, 2022 at 2:14

2 Answers 2

2

Try with groupby and save the dfs into dict

d = {x : y for x , y in df.groupby(('Set X','File'))}
d[1]
Out[190]: 
  Set X             Set Y         Set Z
   File    Sheet Duration Cost Distance
0     1  Sheet 1        2    7        2
1     1  Sheet 1        5    5        4
Sign up to request clarification or add additional context in comments.

Comments

0

If you groupby the tuple of keys, you can iterate through them and pull out each group into dataframes:

data = {
    'File':[1,1,2,2,3],
    'Sheet':['Sheet 1','Sheet 1','Sheet 3','Sheet3','Sheet5'],
    'Duration':[2,5,3,7,9],
    'Cost':[7,5,8,9,3],
    'Distance':[2,4,5,7,5]
}
df = pd.DataFrame(data) 
df.columns=[['Set X','Set X','Set Y','Set Y','Set Z'],['File','Sheet','Duration','Cost','Distance']]

groups = df.groupby(('Set X', 'File'))

df1, df2, df3, = (groups.get_group(g) for g in groups.groups)

This will give expected results like, df1:

    Set X           Set Y              Set Z
    File    Sheet   Duration    Cost   Distance
0   1       Sheet 1 2           7      2
1   1       Sheet 1 5           5      4

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.