Sergio Espejo Sergio Espejo - 1 year ago 67
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['year'] = pd.DatetimeIndex(df['Date']).year
df['month'] = pd.DatetimeIndex(df['Date']).month

Answer Source


df1 = df[df.month.isin([5, 6, 7, 8])]
df2 = df[df.month.isin([4, 9, 10, 11])]
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download