如何使用 pandas.DataFrame.sum() 按列索引号而不是索引字符串对列求和
how to use pandas.DataFrame.sum() to sum columns by column-indexnumber rather than index-string
我有以下文件,其中每个列名都有几列。
所以,我不能通过字符串引用列。我想我应该使用索引号。但是,我不知道该怎么做。
我尝试使用以下代码对“D”列(“Banana”)求和:
column_list=[3]
x = file[column_list].sum(axis='rows')
x
但我收到一条错误消息:
KeyError: "None of [Int64Index([3], dtype='int64')] are in the [columns]"
file.iloc[:,3].sum()
应该可以解决问题。但是你会过得更好,保持你的数据帧一致,我。 e.避免重复的列名。
我有以下文件,其中每个列名都有几列。 所以,我不能通过字符串引用列。我想我应该使用索引号。但是,我不知道该怎么做。
我尝试使用以下代码对“D”列(“Banana”)求和:
column_list=[3]
x = file[column_list].sum(axis='rows')
x
但我收到一条错误消息:
KeyError: "None of [Int64Index([3], dtype='int64')] are in the [columns]"
file.iloc[:,3].sum()
应该可以解决问题。但是你会过得更好,保持你的数据帧一致,我。 e.避免重复的列名。