让我们考虑以下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"
的算术平均值。
我的第一种方法是在 Pandas 中使用 groupby()
和 mean()
的组合:
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
有人知道如何做到这一点吗?