计算列中同一行内多个值的平均值

Calculate the mean of several values within the same row in a column

我有这个 df 有几行在百分比列中有一个以上的值。有些行只有一个值,有些行有 2 - 3 个值。我想计算具有多个值的行的平均值。

    location            Ethnic                          Percent
0   Beaches-East York   English , Scottish , Canadian   19.7 , 18.9 , 24.2

因为只有 4 行有多个值,所以我以非常业余的方式逐行计算并替换值。如果我要计算 50 行,更有效的方法是什么?

我的业余尝试:

import statistics

print(statistics.mean([19.7,18.9,24.2]))

您可以在百分比列中使用应用函数来完成

df["mean_percent"] = df.Percent.apply(lambda x: pd.np.mean([float(p) for p in  x.split(",")]))
print(df)