Python 大熊猫获得带有舍入值的列平均值/平均值
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/31037298/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
pandas get column average/mean with round value
提问by PepperoniPizza
I can't get the average or mean of a column in pandas. A have a dataframe. Neither of things I tried below gives me the average of the column weight
我无法获得熊猫列的平均值或平均值。A 有一个数据框。我在下面尝试的任何事情都没有给我列的平均值weight
>>> allDF
ID birthyear weight
0 619040 1962 0.1231231
1 600161 1963 0.981742
2 25602033 1963 1.3123124
3 624870 1987 0.94212
The following returns several values, not one:
以下返回多个值,而不是一个:
allDF[['weight']].mean(axis=1)
So does this:
这样做也是如此:
allDF.groupby('weight').mean()
采纳答案by DSM
If you only want the mean of the weight
column, select the column (which is a Series) and call .mean()
:
如果您只想要weight
列的平均值,请选择列(这是一个系列)并调用.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["weight"].mean()
Out[480]: 0.83982437500000007
回答by nainometer
Do try to give print (df.describe())
a shot. I hope it will be very helpful to get an overall description of your dataframe.
一定要试一试print (df.describe())
。我希望对您的数据框进行全面描述会非常有帮助。
回答by Chandu
Try df.mean(axis=0)
, axis=0
argument calculates the column wise mean of the dataframe so the result will be axis=1
is row wise mean so you are getting multiple values.
尝试df.mean(axis=0)
,axis=0
参数计算数据框的列均值,因此结果将axis=1
是行均值,因此您将获得多个值。
回答by Arun Singh
you can use
您可以使用
df.describe()
you will get basic statistics of the dataframe and to get mean of specific column you can use
您将获得数据框的基本统计信息并获得您可以使用的特定列的平均值
df["columnname"].mean()
回答by Nikos Tavoularis
You can also access a column using the dot notation (also called attribute access) and then calculate its mean:
您还可以使用点表示法(也称为属性访问)访问列,然后计算其平均值:
df.your_column_name.mean()
回答by Harvey
Mean for each column in df
:
中每列的平均值 df
:
A B C
0 5 3 8
1 5 3 9
2 8 4 9
df.mean()
A 6.000000
B 3.333333
C 8.666667
dtype: float64
and if you want average of all columns:
如果你想要所有列的平均值:
df.stack().mean()
6.0
回答by davidbilla
You can use either of the two statements below:
您可以使用以下两个语句之一:
numpy.mean(df['col_name'])
# or
df['col_name'].mean()
回答by HUNTER
You can easily followthe following code
`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())
回答by Md. Tanvir Raihan
Additionally if you want to get the round
value after finding the mean
.
此外,如果您想round
在找到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