如何用熊猫计算逗号分隔列的均值?

时间:2020-10-01 17:45:33

标签: python pandas

让我们考虑以下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()的组合:

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

有人知道怎么做吗?

1 个答案:

答案 0 :(得分:4)

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

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中的字符串在单个组中具有不同的列数,则会出错。

您可能可以在相当大的程度上清除我的功能