简体   繁体   English

Python Pandas DataFrame从列中选择行

[英]Python pandas dataframe select rows from columns

In an Excel sheet with columns Rainfall / Year / Month, I want to sum rainfall data per year. 在带有“降雨/年/月”列的Excel工作表中,我想对每年的降雨数据求和。 That is, for instance, for the year 2000, from month 1 to 12, summing all the Rainfall cells into a new one. 例如,对于2000年,从第1个月到第12个月,将所有降雨单元合计为一个新单元。 I tried using pandas in Python but cannot manage (just started coding). 我尝试在Python中使用熊猫,但无法管理(刚刚开始编码)。 How can I proceed? 我该如何进行? Any help is welcome, thanks! 欢迎任何帮助,谢谢!

Here the head of the data (which has been downloaded): 这里是数据头(已下载):

   rainfall (mm)  \tyear   month  country   iso3   iso2
0      120.54000    1990       1      ECU    NaN    NaN
1      231.15652    1990       2      ECU    NaN    NaN
2      136.62088    1990       3      ECU    NaN    NaN
3      203.47653    1990       4      ECU    NaN    NaN
4      164.20956    1990       5      ECU    NaN    NaN

Use groupby and aggregate sum if need sum of all years: 如果需要所有年份的总和,请使用groupbysum

df = df.groupby('\tyear')['rainfall (mm)'].sum()

But if need only one value: 但是如果只需要一个值:

df.loc[df['\tyear'] == 2000, 'rainfall (mm)'].sum()

If you just want the year 2000, use 如果您只想要2000年,请使用

df[df['\tyear'] == 2000]['rainfall (mm)'].sum()

Otherwise, jezrael's answer is nice because it sums rainfall (mm) for each distinct value of \\tyear . 否则,jezrael的答案很好,因为对于每个\\tyear不同值,jezrael的总rainfall (mm)\\tyear

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM