如何汇总熊猫中的特定列

时间:2019-08-29 08:07:34

标签: python-3.x pandas

我试图找到我的csv文件的平均特定列,该列已被熊猫读入数据框。我想找到2018年7月至2018年9月的均值,然后显示它们。

Variable | 2018 Jul | 2018 Aug | 2018 Sep | 2018 Oct | 2018 Nov | 2018 Dec |  ....
GDP      | 100      | 200      | 300      | 400      | 500      | 600      | ....

我尝试使用此代码,但以'Nan'结尾

vam2['2018 Jul-Sep'] = vam2.iloc[0:1, :2].mean()

vam2

我认为找到平均值后,“ 2018年7月至9月”应该为200。

Variable | 2018 Jul | 2018 Aug | 2018 Sep | 2018 Oct | 2018 Nov | 2018 Dec | 2018 Jul-Sep |   ....
GDP      | 100      | 200      | 300      | 400      | 500      | 600      | 200          | ....

2 个答案:

答案 0 :(得分:2)

如果需要平均所有行,我认为应该删除 Member m = await manager.Members.AddOrUpdateAsync(ConfigurationManager.AppSettings["MailChimpListId"].ToString(), new Member { EmailAddress = _email, EmailType = "html", Status = Status.Pending}); m = await manager.Members.AddOrUpdateAsync(ConfigurationManager.AppSettings["MailChimpListId"].ToString(), new Member { EmailAddress = _email, EmailType = "html", Status = Status.Subscribed }); ,并在每行0:1上添加axis=1

如果mean是列:

Variable

如果#for convert to numeric vam2.iloc[:, 1:] = vam2.iloc[:, 1:].apply(pd.to_numeric, errors='coerce') vam2['2018 Jul-Sep'] = vam2.iloc[:, 1:4].mean(axis=1) print (vam2) Variable 2018 Jul 2018 Aug 2018 Sep 2018 Oct 2018 Nov 2018 Dec \ 0 GDP 100 200 300 400 500 600 2018 Jul-Sep 0 200.0 是索引:

Variable

答案 1 :(得分:0)

df['2018 Jul-Sep'] = df.iloc[:,1:4].mean(axis=1)