我无法得到熊猫列的平均值或平均值。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()

当前回答

此外,如果你想在找到平均值后得到整数。

#Create a DataFrame
df1 = {
    'Subject':['semester1','semester2','semester3','semester4','semester1',
               'semester2','semester3'],
   'Score':[62.73,47.76,55.61,74.67,31.55,77.31,85.47]}
df1 = pd.DataFrame(df1,columns=['Subject','Score'])

rounded_mean = round(df1['Score'].mean()) # specified nothing as decimal place
print(rounded_mean) # 62

rounded_mean_decimal_0 = round(df1['Score'].mean(), 0) # specified decimal place as 0
print(rounded_mean_decimal_0) # 62.0

rounded_mean_decimal_1 = round(df1['Score'].mean(), 1) # specified decimal place as 1
print(rounded_mean_decimal_1) # 62.2

其他回答

你也可以使用点符号(也称为属性访问)访问一个列,然后计算它的平均值:

df.your_column_name.mean()

请尝试尝试print (df.describe())。我希望这将是非常有用的,以获得您的数据框架的整体描述。

你可以使用agg (aggregate)方法:

df.agg('mean')

可以应用多个统计数据:

df.agg(['mean', 'max', 'min'])

您可以轻松地执行以下代码

import pandas as pd 
import numpy as np 
        
classxii = {'Name':['Karan','Ishan','Aditya','Anant','Ronit'],
            'Subject':['Accounts','Economics','Accounts','Economics','Accounts'],
            'Score':[87,64,58,74,87],
            'Grade':['A1','B2','C1','B1','A2']}

df = pd.DataFrame(classxii,index = ['a','b','c','d','e'],columns=['Name','Subject','Score','Grade'])
print(df)

#use the below for mean if you already have a dataframe
print('mean of score is:')
print(df[['Score']].mean())

如果你只想要权重列的平均值,选择该列(它是一个Series)并调用.mean():

In [479]: df
Out[479]: 
         ID  birthyear    weight
0    619040       1962  0.123123
1    600161       1963  0.981742
2  25602033       1963  1.312312
3    624870       1987  0.942120

In [480]: df.loc[:, 'weight'].mean()
Out[480]: 0.83982437500000007