如何在熊猫中标记成对的组?

时间:2017-07-30 06:30:55

标签: python python-2.7 pandas pandas-groupby

我有这个数据框:

>>> df = pd.DataFrame({'A': [1, 2, 1, np.nan, 2, 2, 2], 'B': [2, 1, 2, 2.0, 1, 1, 2]})
>>> df
     A    B
0  1.0  2.0
1  2.0  1.0
2  1.0  2.0
3  NaN  2.0
4  2.0  1.0
5  2.0  1.0
6  2.0  2.0

我需要在第三列" group id"上识别成对组(A,B),以得到类似的结果:

>>> df
     A    B  grup id                        explanation
0  1.0  2.0      1.0  <- group (1.0, 2.0), first group 
1  2.0  1.0      2.0  <- group (2.0, 1.0), second group
2  1.0  2.0      1.0  <- group (1.0, 2.0), first group 
3  NaN  2.0      NaN  <- invalid group                 
4  2.0  1.0      2.0  <- group (2.0, 1.0), second group
5  2.0  1.0      2.0  <- group (2.0, 1.0), second group
6  2.0  2.0      3.0  <- group (2.0, 2.0), third group 

我怎样才能在熊猫中有效地做到这一点?

一个想法是首先构建一个组合列(A,B),然后识别该列中的唯一值并将它们映射回我的数据帧。但我怀疑groupby()方法会更快(更优雅)。

我试过了:

>>> df.groupby(['A','B']).count()
Empty DataFrame
Columns: []
Index: [(1.0, 2.0), (2.0, 1.0), (2.0, 2.0)]

所以这个groupby()的索引列出了我需要的所有组。但那么如何计算它们并将它们映射回我的数据框?

1 个答案:

答案 0 :(得分:3)

您可以使用GroupBy.ngroup(pandas 0.20.2 +):

print (df.groupby(['A','B']).ngroup())
0    0
1    1
2    0
3   -1
4    1
5    1
6    2
dtype: int64

df['grup id'] = df.groupby(['A','B']).ngroup().replace(-1,np.nan).add(1)
print (df)
     A    B  grup id
0  1.0  2.0      1.0
1  2.0  1.0      2.0
2  1.0  2.0      1.0
3  NaN  2.0      NaN
4  2.0  1.0      2.0
5  2.0  1.0      2.0
6  2.0  2.0      3.0

类似于替换-1并添加1

df['grup id'] = df.groupby(['A','B']).ngroup()
df['grup id'] = np.where(df['grup id'] == -1, np.nan, df['grup id'] + 1)
print (df)
     A    B  grup id
0  1.0  2.0      1.0
1  2.0  1.0      2.0
2  1.0  2.0      1.0
3  NaN  2.0      NaN
4  2.0  1.0      2.0
5  2.0  1.0      2.0
6  2.0  2.0      3.0

对于最旧版本的pandas(低于0.20.2):

df['grup id'] = df.groupby(["A","B"]).grouper.group_info[0]
df['grup id'] = np.where(df['grup id'] == -1, np.nan, df['grup id'] + 1)
print (df)
     A    B  grup id
0  1.0  2.0      1.0
1  2.0  1.0      2.0
2  1.0  2.0      1.0
3  NaN  2.0      NaN
4  2.0  1.0      2.0
5  2.0  1.0      2.0
6  2.0  2.0      3.0