Reputation: 527
For example; I have a data frame like this
ID v1 v2 v3
1 0 0 1
1 1 1 1
1 1 0 1
1 1 1 0
2 0 1 1
2 0 0 1
2 1 1 0
3 1 0 1
3 1 1 1
What I'm interested in is to get a list of data frame such that each data-frame is a subset of original based on the groupby['ID']
So looking for something returns a list of data frame result
in which
result[0]
ID v1 v2 v3
1 0 0 1
1 1 1 1
1 1 0 1
1 1 1 0
result[1]
ID v1 v2 v3
2 0 1 1
2 0 0 1
2 1 1 0
result[2]
ID v1 v2 v3
3 1 0 1
3 1 1 1
Right now, what I'm doing is loop iteration through ID
for id in df['ID']:
result.append(df[df['ID'] == id])
But there might be lots of ID
and this not very efficient (note that each ID
might have different number of data points). Any thoughts ? Thanks
Upvotes: 1
Views: 134
Reputation: 323316
Check with
d = dict([*df.groupby('ID')])
d[1]
Out[160]:
ID v1 v2 v3
0 1 0 0 1
1 1 1 1 1
2 1 1 0 1
3 1 1 1 0
Upvotes: 1