我有一个数据框:

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/

10-12 20:11