Home > other >  separate a dataframe into multiple dataframes using specific values
separate a dataframe into multiple dataframes using specific values

Time:03-04

I have the following 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

I want to group by the season column and generate three different dataframes

Expected outcome:

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

I am using df[df['season']==0] but I think it is too static

Any ideas?

CodePudding user response:

You can use groupby and a dictionary comprehension:

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

output:

{'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}

Access:

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

Or, maybe better, as list:

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

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