I have a dataframe (called df) and want to split it into multiple dataframes based on the values in one of the columns.
I think the syntax would be:
month = df.month.unique().tolist()
for item in month:
[item] = df[df[month]==[item]]
I have a dataframe (called df) and want to split it into multiple dataframes based on the values in one of the columns.
I think the syntax would be:
month = df.month.unique().tolist()
for item in month:
[item] = df[df[month]==[item]]
The DataFrameGroupBy object is an iterator that returns sub-DataFrames:
for month, subdf in df.groupby(['month']):
...
If instead you want random access to the sub-DataFrames, specified by month,
you could change the month
column into an index:
df = df.set_index(['month'])
and then you could select rows by month with:
df.loc[month]
For example,
In [4]: df = pd.DataFrame({'month': ['Jan','Jan','Feb'], 'val':[1,2,3]})
In [6]: df = df.set_index(['month'])
Given this DataFrame:
In [7]: df
Out[7]:
val
month
Jan 1
Jan 2
Feb 3
This selects the rows where the month (index) is 'Jan'
:
In [8]: df.loc['Jan']
Out[8]:
val
month
Jan 1
Jan 2