I've tried to split my dataframe to groups
df = pd.DataFrame({'A' : ['foo', 'bar', 'foo', 'bar',
'foo', 'bar', 'foo', 'foo'],
'B' : ['1', '2', '3', '4',
'5', '6', '7', '8'],
})
grouped = df.groupby('A')
I get 2 groups
A B
0 foo 1
2 foo 3
4 foo 5
6 foo 7
7 foo 8
A B
1 bar 2
3 bar 4
5 bar 6
Now I want to reset indexes for each group separately
print grouped.get_group('foo').reset_index()
print grouped.get_group('bar').reset_index()
Finally I get the result
A B
0 foo 1
1 foo 3
2 foo 5
3 foo 7
4 foo 8
A B
0 bar 2
1 bar 4
2 bar 6
Is there better way how to do this? (For example: automatically call some method for each group)
See Question&Answers more detail:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…