使用特定值将数据帧分成多个数据帧

separate a dataframe into multiple dataframes using specific values

我有以下dataframe

df = pd.DataFrame({'season': ['0', '0', '1', '1', '2'],
                   'fruits': ['orange', 'mango', 'apple', 'grapes', 'NaN'],
                   'price': ['40', '80', 'NaN', '40', '30']
                   })

   season fruits  price
0    0    orange  40
1    0    mango   80
2    1    apple   NaN
3    1    grapes  40
4    2    NaN     30

我想按季节列分组并生成三个不同的dataframes

预期结果:

df1:
   season fruits  price
0    0    orange  40
1    0    mango   80

df2:
   season fruits  price
2    1    apple   NaN
3    1    grapes  40

df3:
   season fruits  price
4    2    NaN     30

我正在使用 df[df['season']==0] 但我认为它太静态了

有什么想法吗?

您可以使用 groupby 和字典理解:

dfs = {f'df{int(k)+1}': g for k,g in df.groupby('season')}

输出:

{'df1':   season  fruits price
 0      0  orange    40
 1      0   mango    80,
 'df2':   season  fruits price
 2      1   apple   NaN
 3      1  grapes    40,
 'df3':   season fruits price
 4      2    NaN    30}

访问:

dfs['df1']
#   season  fruits price
# 0      0  orange    40
# 1      0   mango    80

或者,也许更好,如列表:

dfs = [g for _,g in df.groupby('season')]

dfs[0]
#   season  fruits price
# 0      0  orange    40
# 1      0   mango    80