Skip to main content
 首页 » 编程设计

python之使用不同的 {key :value} dictionaries 运行 Groupby

2025年05月04日16lyhabc

在下面的代码中:

import pandas as pd 
import numpy as np 
import random 
 
sz = 50 
df = pd.DataFrame({'Group': pd.Series(random.choice(['A', 'B']) for _ in range(sz)), 
              'Key': pd.Series(np.random.randint(2, high=5, size=sz))}) 
 
dictforA = {2: 0.1, 3: 0.8, 4: 0.2} 
dictforB = {3: 0.9} 

...想要分配一个名为 Value 的新列,该列基于其各自的字典。缺失值将为 NaN。

代码: df.assign(Value=df.groupby('Group').apply(lambda x: np.where(x.index == 'A', dictforA[x.Key], dictforB[x.Key]) ))

给予

TypeError: 'Series' 对象是可变的,因此它们不能被散列

我哪里错了?

请您参考如下方法:

你可以创建一个从 Group 到你的字典的映射器并使用 pd.Series.map

mapper = {'A': dictforA,  
          'B': dictforB} 
 
df['Value'] = df.groupby('Group').Key.apply(lambda s: s.map(mapper[s.name])) 

>>> print(df.head(10)) 
 
  Group  Key  Value 
0     B    3    0.9 
1     A    2    0.1 
2     B    3    0.9 
3     A    3    0.8 
4     A    3    0.8 
5     A    2    0.1 
6     B    2    NaN 
7     B    2    NaN 
8     A    4    0.2 
9     A    2    0.1