gpt4 book ai didi

python - 从 MultiIndex Pandas 升序/重置整数值

转载 作者:太空宇宙 更新时间:2023-11-04 03:07:44 24 4
gpt4 key购买 nike

我有一个数据框:

import pandas as pd

tuples = [('a', 1990),('a', 1994),('a',1996),('b',1992),('b',1997),('c',2001)]
index = pd.MultiIndex.from_tuples(tuples, names = ['Type', 'Year'])
vals = ['This','That','SomeName','This','SomeOtherName','SomeThirdName']
df = pd.DataFrame(vals, index=index, columns=['Whatev'])


df
Out[3]:
Whatev
Type Year
a 1990 This
1994 That
1996 SomeName
b 1992 This
1997 SomeOtherName
c 2001 SomeThirdName

我想添加一列对应于每个“类型”重置的“年份”的升序整数,如下所示:

                  Whatev  IndexInt
Type Year
a 1990 This 1
1994 That 2
1996 SomeName 3
b 1992 This 1
1997 SomeOtherName 2
c 2001 SomeThirdName 1

这是我目前的方法:

grouped = df.groupby(level=0)
unique_loc = []
for name, group in grouped:
unique_loc += range(1,len(group)+1)
joined['IndexInt'] = unique_loc

但这对我来说似乎很难看且令人费解,而且我想它在我正在使用的约 5000 万行数据帧上可能会变慢。有没有更简单的方法?

最佳答案

你可以使用 groupby(level=0) + cumcount() :

In [7]: df['IndexInt'] = df.groupby(level=0).cumcount()+1

In [8]: df
Out[8]:
Whatev IndexInt
Type Year
a 1990 This 1
1994 That 2
1996 SomeName 3
b 1992 This 1
1997 SomeOtherName 2
c 2001 SomeThirdName 1

关于python - 从 MultiIndex Pandas 升序/重置整数值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/39004736/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com