gpt4 book ai didi

python - 如何使用 Pandas 计算逗号分隔列的平均值?

转载 作者:行者123 更新时间:2023-12-03 16:32:32 27 4
gpt4 key购买 nike

让我们考虑以下 CSV 文件 test.csv :

"x","y","A","B"
8000000000,"0,1","0.113948,0.113689",0.114042
8000000000,"0,1","0.114063,0.113823",0.114175
8000000000,"0,1","0.114405,0.114366",0.114524
8000000000,"0,1,2,3","0.167543,0.172369,0.419197,0.427285",0.427576
8000000000,"0,1,2,3","0.167784,0.172145,0.418624,0.426492",0.428736
8000000000,"0,1,2,3","0.168121,0.172729,0.419768,0.427467",0.428578

我的目标是按列对行进行分组 "x""y" ,并计算列的算术平均值 "A""B" .
我的第一种方法是使用 groupby() 的组合。和 mean()在 Pandas 中:
import pandas

if __name__ == "__main__":
data = pandas.read_csv("test.csv", header=0)
data = data.groupby(["x", "y"], as_index=False).mean()
print(data)
运行此脚本会产生以下输出:
            x        y         B
0 8000000000 0,1 0.114247
1 8000000000 0,1,2,3 0.428297
如我们所见,实现了我对单值列的目标 "B"很简单。然而,列 "A"被省略。相反,我想要列 "A"带有一个字符串,其中包含每个逗号分隔值的算术平均值。所需的输出应如下所示:
            x        y                                    A         B
0 8000000000 0,1 0.114139,0.113959 0.114247
1 8000000000 0,1,2,3 0.167816,0.172414,0.419196,0.427081 0.428297
有人知道怎么做这个吗?

最佳答案

您可以创建一个自定义聚合函数,将这些字符串解析为列表,找到每列的平均值,并将它们格式化为字符串:

def string_mean(rows):
data_list = []
for row in rows:
data_list.append([float(item) for item in row.split(",")])
data = np.array(data_list)
return ",".join([f"{item:.6f}" for item in data.mean(axis=0)])

df.groupby(["x", "y"], as_index=False).agg({"A": string_mean, "B": "mean"})
返回
            x        y                                    A         B
0 8000000000 0,1 0.114139,0.113959 0.114247
1 8000000000 0,1,2,3 0.167816,0.172414,0.419196,0.427081 0.428297
请注意,如果 A 中的字符串在单个组中具有不同数量的列,则会出错。
顺便说一句,您可能可以大大清理我上面的功能

关于python - 如何使用 Pandas 计算逗号分隔列的平均值?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/64160661/

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