如何一步一步为所有组重置DataFrame的索引? [英] How to reset a DataFrame's indexes for all groups in one step?
问题描述
我试图将数据框分成几组
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')
我有2个小组
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()
最后我得到了结果
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)
推荐答案
将as_index=False
传递给groupby,则无需reset_index
再次将groupby-d列设置为列:
Pass in as_index=False
to the groupby, then you don't need to reset_index
to make the groupby-d columns columns again:
In [11]: grouped = df.groupby('A', as_index=False)
In [12]: grouped.get_group('foo')
Out[12]:
A B
0 foo 1
2 foo 3
4 foo 5
6 foo 7
7 foo 8
注意:正如指出的(并在上面的示例中看到的那样),上面的索引不是 [0, 1, 2, ...]
,我声称这在实践中将不再重要-如果这样做的话必须经过一些奇怪的圈-它将变得更加冗长,可读性和效率降低...
Note: As pointed out (and seen in the above example) the index above is not [0, 1, 2, ...]
, I claim that this will never matter in practice - if it does you're going to have to just through some strange hoops - it's going to be more verbose, less readable and less efficient...
这篇关于如何一步一步为所有组重置DataFrame的索引?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!