简体   繁体   English

如何使用 pandas.DataFrame.sum() 按 column-indexnumber 而不是 index-string 对列求和

[英]how to use pandas.DataFrame.sum() to sum columns by column-indexnumber rather than index-string

I have the following file where for each column name, there are several columns.我有以下文件,其中每个列名都有几列。 So, I cant refer to columns by string.所以,我不能按字符串引用列。 I think I should use an indexnumber.我想我应该使用索引号。 However, I dont know how to do it.但是,我不知道该怎么做。

在此处输入图片说明

I tried the following code to sum the "D" column ("Banana"):我尝试使用以下代码对“D”列(“Banana”)求和:

column_list=[3]
x = file[column_list].sum(axis='rows')
x

But I get an error message stating:但我收到一条错误消息,指出:

KeyError: "None of [Int64Index([3], dtype='int64')] are in the [columns]"
file.iloc[:,3].sum()

should do the trick.应该做的伎俩。 But you would be much better off, keeping your dataframe consistent, ie avoid duplicate column names.但是你会更好,保持你的数据框一致,即避免重复的列名。

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

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