如何一步重置所有组的DataFrame索引? [英] How to reset a DataFrame's indexes for all groups in one step?

查看:42
本文介绍了如何一步重置所有组的DataFrame索引?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图将我的数据帧分成多个组

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

注意:正如所指出的(在上面的例子中看到的)上面的索引是not [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屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆