关键点:return 里面的分组数据,重新接收
def f(gp):
if gp.iloc[0].name == 0:
gp.loc[0, 'name'] = 'Lily'
return gp
if __name__ == '__main__':
# 测试修改分组后的属性值
test = {'id': [1, 2, 6, 4, 2, 6], \
'name': ['Alice', 'Bob', 'Cindy', 'Eric', 'Helen', 'Grace '], \
'math': [90, 89, 99, 78, 97, 93], 'english': [89, 94, 80, 94, 94, 90]}
df = pd.DataFrame(test)
print(df)
gb = df.groupby(['id'], as_index=False)
new_gp = gb.apply(f)
print(new_gp)
结果如下:
![](https://img.haomeiwen.com/i14371927/aa65dbb5dc389eec.png)
疑问:如果将f函数写成下面这样,也就是少了一层判断,为什么会返回一个双重索引的对象?
def f(gp):
gp.loc[0, 'name'] = 'Lily'
return gp
![](https://img.haomeiwen.com/i14371927/9580adc625e7e9d8.png)
网友评论