问题:熊猫获得列平均值/平均值

我无法获得熊猫列的平均值或均值。有一个数据框。我在下面尝试的任何事情都没有给我该列的平均值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

以下返回几个值,而不是一个:

allDF[['weight']].mean(axis=1)

这样:

allDF.groupby('weight').mean()

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

>>> 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()

回答 0

如果您只想要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

If you only want the mean of the weight column, select the column (which is a Series) and call .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

回答 1

Try df.mean(axis=0)axis=0参数计算数据帧的列均值,因此结果将axis=1是行均值,因此您将获得多个值。

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.


回答 2

尝试尝试一下print (df.describe())。希望对您的数据框有一个总体描述会很有帮助。

Do try to give print (df.describe()) a shot. I hope it will be very helpful to get an overall description of your dataframe.


回答 3

您可以使用

df.describe() 

您将获得数据框的基本统计信息并获取可以使用的特定列的平均值

df["columnname"].mean()

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()

回答 4

您还可以使用点表示法访问列(也称为属性访问),然后计算其均值:

df.your_column_name.mean()

You can also access a column using the dot notation (also called attribute access) and then calculate its mean:

df.your_column_name.mean()

回答 5

每列中的均值 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

以及是否要平均所有列:

df.stack().mean()
6.0

Mean for each column in 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

回答 6

另外,如果要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

Additionally if you want to get the round value after finding the 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

回答 7

您可以使用以下两个语句之一:

numpy.mean(df['col_name'])
# or
df['col_name'].mean()

You can use either of the two statements below:

numpy.mean(df['col_name'])
# or
df['col_name'].mean()

回答 8

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())
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())

回答 9

您可以简单地进行以下操作:df.describe()将为您提供所需的所有相关详细信息,但是要查找特定列的最小值,最大值或平均值(在您的情况下为“权重”),请使用:

    df['weights'].mean(): For average value
    df['weights'].max(): For maximum value
    df['weights'].min(): For minimum value

You can simply go for: df.describe() that will provide you with all the relevant details you need, but to find the min, max or average value of a particular column (say ‘weights’ in your case), use:

    df['weights'].mean(): For average value
    df['weights'].max(): For maximum value
    df['weights'].min(): For minimum value

声明:本站所有文章,如无特殊说明或标注,均为本站原创发布。任何个人或组织,在未征得本站同意时,禁止复制、盗用、采集、发布本站内容到任何网站、书籍等各类媒体平台。如若本站内容侵犯了原著者的合法权益,可联系我们进行处理。