0

I have a dataframe:

Type Sub_Type Count
A AA 10
A AA 5
B AA 9
B BA 4
B BA 5
A BA 2
B CA 8
B CA 5
A CA 3

I want the dataframes to be split according to the Sub_type.

The output dataframes should be:

Type Sub_Type Count
A AA 10
A AA 5
B AA 9
Type Sub_Type Count
B BA 4
B BA 5
A BA 2
Type Sub_Type Count
B CA 8
B CA 5
A CA 3
kanika
  • 37
  • 4
  • Have you tried writing some code for it? If so, please share the code you have and explain which part is not working the way you want. I'm also curious what kind of further processing you're going to do on these DataFrames. Do you really need them to be separate? It seems likely to lead to inefficiency later in your pipeline. – John Zwinck Feb 19 '23 at 17:07

1 Answers1

1
import pandas as pd

df = pd.DataFrame({'Type': ["A", "A", "B", "B", "B", "A"],
                    'Sub_Type': ["AA", "AA", "BA", "BA", "AA", "BA"],
                    'Count': [3, 9, 3, 4, 3, 1],
                    })

datdict = {}
i = 0
for frame, data in df.groupby(['Type', 'Sub_Type']):
    datdict[i] = data
    i+=1
Dataframe 0 for instance :
print(pd.DataFrame(datdict[0]))

  Type Sub_Type  Count
0    A       AA      3
1    A       AA      9
Datadict contains :
>>> datdict
{0:   Type Sub_Type  Count
0    A       AA      3
1    A       AA      9, 1:   Type Sub_Type  Count
5    A       BA      1, 2:   Type Sub_Type  Count
4    B       AA      3, 3:   Type Sub_Type  Count
2    B       BA      3
3    B       BA      4}
Laurent B.
  • 1,653
  • 1
  • 7
  • 16
  • I need the separate dataframes to be created and printed automatically. The loop should do it. Is there any way? – kanika Feb 20 '23 at 03:26
  • They are already created like they are like objects put in the dictionary. Access is by classical method. Ex: ```df1 = pd.DataFrame(datdict[0])``` – Laurent B. Feb 20 '23 at 20:32