'separate a dataframe into multiple dataframes using specific values

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?



Sources

This article follows the attribution requirements of Stack Overflow and is licensed under CC BY-SA 3.0.

Source: Stack Overflow

Solution Source