Sergio Espejo Sergio Espejo - 3 months ago 8
Python Question

How do I create dataframes from the groups that have been arranged by groupby?

I have separated my data set by months, there are a total of 8 groups for 8 different months, I want to create two separate dataframes which one will include the data that is found on months:5,6,7,8 and the other dataframe will include the months:4,9,10,11. How can I tell groupby to create these two separate datasets?

df['Date']=pd.to_datetime(df['Date'],format='%m/%d/%y')
df['year'] = pd.DatetimeIndex(df['Date']).year
df['month'] = pd.DatetimeIndex(df['Date']).month
df2=df.groupby('month')

Answer

Try:

df1 = df[df.month.isin([5, 6, 7, 8])]
df2 = df[df.month.isin([4, 9, 10, 11])]
Comments