Reputation: 1694
I'd like to break a dataframe into several dataframes base on the values in the Group
column.
generate data
np.random.seed(0)
groups = ('Group 1', 'Group 2', 'Group 3')
means = ('Low', 'High', 'Mid')
index = pd.MultiIndex.from_product([groups, means], names=['Group', 'Mean'])
values = np.random.randint(low=20, high=100, size=len(index))
data = pd.DataFrame(data={'val': values}, index=index).reset_index()
Group Mean val
Group 1 Low 64
Group 1 High 67
Group 1 Mid 84
Group 2 Low 87
Group 2 High 87
Group 2 Mid 29
Group 3 Low 41
Group 3 High 56
Group 3 Mid 90
I'm able to extract a single group
grouped = data.groupby(data.Group)
g1 = grouped.get_group("Group 1")
But I'd like to split the data into individual dataframes
Expected output:
data_Group1
Group Mean val
Group 1 Low 64
Group 1 High 67
Group 1 Mid 84
data_Group2
Group 2 Low 87
Group 2 High 87
Group 2 Mid 29
and same for Group 3
The real data is a lot bigger than this, so it will be great to automate the process. thanks
Upvotes: 1
Views: 143
Reputation: 402413
You can call GroupBy
and iterate over groups. Is a dictionary what you're looking for?
groups = {group_name : group for group_name, group in df.groupby('Group')}
groups['Group 1']
Group Mean val
0 Group 1 Low 64
1 Group 1 High 67
2 Group 1 Mid 84
Upvotes: 1