将一个数据框拆分为多个数据框

问题描述:

我有一个数据框;我使用groupby对其进行了拆分.我了解这会将数据帧拆分为多个数据帧.如何根据组取回这些单独的数据框并相应地命名?因此,如果说df.groupby(['A','B']) 并且A具有值A1,B具有值B1-B4,我想取回这四个数据帧callefdf_A1B1..df_A1B1,df_A1B2 ... df_A1B4?

I have a dataframe; I split it using groupby. I understand this splits the dataframes into multiple dataframes. How can I get back those individual dataframes , based on the groups and name them accordingly? So if said df.groupby(['A','B']) and A has values A1, and B has values B1-B4, I want to get back those 4 dataframes callefdf_A1B1..df_A1B1, df_A1B2...df_A1B4?

这可以通过locals完成,但不推荐

This can be done by locals but not recommend

variables = locals()
for i,j in df.groupby(['A','B']):
    variables["df_{0[0]}{0[1]}".format(i)] = j
df_01
Out[332]: 
   A  B              C
0  0  1  a-1524112-124

使用dict是正确的方法

Using dict is the right way

{"df_{0[0]}{0[1]}".format(i) : j for i,j in df.groupby(['A','B'])}