我无法得到熊猫列的平均值或平均值。A有一个数据框架。下面我尝试的两种方法都没有给出列权值的平均值
>>> allDF
ID birthyear weight
0 619040 1962 0.1231231
1 600161 1963 0.981742
2 25602033 1963 1.3123124
3 624870 1987 0.94212
下面返回多个值,而不是一个:
allDF[['weight']].mean(axis=1)
这个也一样:
allDF.groupby('weight').mean()
请注意,它首先需要是数值数据类型。
import pandas as pd
df['column'] = pd.to_numeric(df['column'], errors='coerce')
接下来,使用describe()找到一列或所有数字列的平均值。
df['column'].mean()
df.describe()
来自describe的结果示例:
column
count 62.000000
mean 84.678548
std 216.694615
min 13.100000
25% 27.012500
50% 41.220000
75% 70.817500
max 1666.860000
请注意,它首先需要是数值数据类型。
import pandas as pd
df['column'] = pd.to_numeric(df['column'], errors='coerce')
接下来,使用describe()找到一列或所有数字列的平均值。
df['column'].mean()
df.describe()
来自describe的结果示例:
column
count 62.000000
mean 84.678548
std 216.694615
min 13.100000
25% 27.012500
50% 41.220000
75% 70.817500
max 1666.860000