1

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?

1 Answer 1

2

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
Sign up to request clarification or add additional context in comments.

8 Comments

I have always thought that we cannot do anything else than apply on a groupby. Never thought that you can just iterate on it. Thanks @mozway !
@pac you can actually do much more ;)
@mozway can you explain the code with the list implementation? Also how you access the generated dataframes without everytime to have to indexing. For example for the first dataframe i execute df1=pd.Dataframe(dfs[0]) but it return me an erro
this would be df1 = dfs[0], the values are already DataFrames, but honestly, use the container it is bad practice to try to generate variable names automatically
why is a bad practise?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.