簡體   English   中英

按 pandas 中的多列數據框分組並獲取列的平均值

[英]Group by multiple column data frame in pandas and get mean value of a column

我有一個像這樣的 dataframe。

輸入:

      Country  Year  AvgTemperature
1826  Algeria  2000  43.9
1827  Algeria  2000  46.5
.
.
7826  Algeria  2016 72.2
7827  Algeria  2016 69.4
.
.
858661 Poland 2000  63.6
858662 Poland 2000  61.9
.
.
857763 Poland 2015  34.8
857764 Poland 2015  39.2
...

我希望 output 按年份和國家以及 AvgTemperature 列的平均值分組。 所以output是這樣的:

      Country  Year  AvgTemperature
1826  Algeria  2000  45.5
.
.
7826  Algeria  2016 70.9
.
.
858661 Poland 2000  62.8
.
.
857763 Poland 2015  37
...

到目前為止,我已經嘗試過:

aggregation_functions = {'AvgTemperature': 'mean'}
df_new = df.groupby(df['Year', 'Country']).aggregate(aggregation_functions)

但收到此錯誤: KeyError: ('Year', 'Country')

df_new = df.groupby(['Year', 'Country']).aggregate(aggregation_functions)
# Import Module
import pandas as pd

# Data Import and Pre-Process
df = pd.DataFrame({'Country':['Algeria','Algeria','Algeria','Algeria','Poland','Poland','Poland','Poland'],
'Year':['2000','2000','2016','2016','2000','2000','2015','2015'],
'AvgTemperature':[43.9,46.5,72.2,69.4,63.6,61.9,34.8,39.2]})
df_v2 = df.groupby(['Country','Year'])['AvgTemperature'].mean().reset_index()

# Output Display
df_v2

嗨 Ferdous,請嘗試上面的代碼,如果您有任何問題,請告訴我

謝謝萊昂

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM