gpt4 book ai didi

python - 用数据框中组的平均值替换列值

转载 作者:行者123 更新时间:2023-11-28 20:14:07 27 4
gpt4 key购买 nike

我有一个 DataFrame 作为

Page    Line    y
1 2 3.2
1 2 6.1
1 3 7.1
2 4 8.5
2 4 9.1

我必须用组中的平均值替换 y 列。我可以使用此代码使用一列进行分组。

df['y'] = df['y'].groupby(df['Page'], group_keys=False).transform('mean') 

我正在尝试通过“Page”和“Line”分组来替换 y 的值。像这样,

Page    Line    y
1 2 4.65
1 2 4.65
1 3 7.1
2 4 8.8
2 4 8.8

我在此站点上搜索了很多答案,但找不到此应用程序。将 python3 与 Pandas 一起使用。

最佳答案

您需要列名列表,groupby参数 by:

by : mapping, function, label, or list of labels

Used to determine the groups for the groupby. If by is a function, it’s called on each value of the object’s index. If a dict or Series is passed, the Series or dict VALUES will be used to determine the groups (the Series’ values are first aligned; see .align() method). If an ndarray is passed, the values are used as-is determine the groups. A label or list of labels may be passed to group by the columns in self. Notice that a tuple is interpreted a (single) key.

df['y'] = df.groupby(['Page', 'Line'])['y'].transform('mean') 
print (df)
Page Line y
0 1 2 4.65
1 1 2 4.65
2 1 3 7.10
3 2 4 8.80
4 2 4 8.80

你的解决方案应该改为这个语法糖 - pass Series in list:

df['y'] = df['y'].groupby([df['Page'], df['Line']]).transform('mean') 

关于python - 用数据框中组的平均值替换列值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/51043372/

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