【问题标题】:Pandas: sum up multiple columns into one column without last column熊猫:将多列汇总为一列,没有最后一列
【发布时间】:2017-06-23 03:42:39
【问题描述】:

如果我有一个类似于这个的数据框

Apples   Bananas   Grapes   Kiwis
2        3         nan      1
1        3         7        nan
nan      nan       2        3

我想添加这样的列

Apples   Bananas   Grapes   Kiwis   Fruit Total
2        3         nan      1        6
1        3         7        nan      11
nan      nan       2        3        5

我猜你可以使用df['Apples'] + df['Bananas'] 等等,但我的实际数据框比这大得多。我希望像df['Fruit Total']=df[-4:-1].sum 这样的公式可以在一行代码中解决问题。然而这并没有奏效。有没有办法在不明确总结所有列的情况下做到这一点?

【问题讨论】:

标签: python pandas sum


【解决方案1】:

您可以先选择iloc,然后再选择sum

df['Fruit Total']= df.iloc[:, -4:-1].sum(axis=1)
print (df)
   Apples  Bananas  Grapes  Kiwis  Fruit Total
0     2.0      3.0     NaN    1.0          5.0
1     1.0      3.0     7.0    NaN         11.0
2     NaN      NaN     2.0    3.0          2.0

所有列的总和使用:

df['Fruit Total']= df.sum(axis=1)

【讨论】:

  • 优秀。 iloc 是我一直在寻找的东西。
  • 这个答案没有添加最后一列,因此有点混乱。
  • @JinhuaWang - 标题已更改。
  • 哦,好吧,对不起
【解决方案2】:

可以在不知道列数甚至没有 iloc 的情况下做到这一点:

print(df)
   Apples  Bananas  Grapes  Kiwis
0     2.0      3.0     NaN    1.0
1     1.0      3.0     7.0    NaN
2     NaN      NaN     2.0    3.0

cols_to_sum = df.columns[ : df.shape[1]-1]

df['Fruit Total'] = df[cols_to_sum].sum(axis=1)

print(df)
   Apples   Bananas Grapes  Kiwis   Fruit Total
0  2.0      3.0     NaN     1.0     5.0
1  1.0      3.0     7.0     NaN     11.0
2  NaN      NaN     2.0     3.0     2.0

【讨论】:

  • 我喜欢这个,因为如果我决定扩展我的数据框,它不需要重新编码
【解决方案3】:

这可能对初学者有帮助,所以为了完整起见,如果您知道列名(例如它们在列表中),您可以使用:

column_names = ['Apples', 'Bananas', 'Grapes', 'Kiwis']
df['Fruit Total']= df[column_names].sum(axis=1)

这使您可以灵活地选择使用哪些列,因为您只需操作列表column_names,并且您可以执行诸如仅选择名称中带有字母“a”的列之类的操作。这样做的另一个好处是,人们更容易通过列名了解他们在做什么。将此与 list(df.columns) 结合使用,以列表格式获取列名。因此,如果您想删除最后一列,您所要做的就是:

column_names = list(df.columns)
df['Fruit Total']= df[column_names[:-1]].sum(axis=1)

【讨论】:

    【解决方案4】:

    在原始 df 上使用 df['Fruit Total']= df.iloc[:, -4:-1].sum(axis=1) 不会添加最后一列('Kiwis'),您应该使用 df.iloc[:, -4:] 来选择所有列:

    print(df)
       Apples  Bananas  Grapes  Kiwis
    0     2.0      3.0     NaN    1.0
    1     1.0      3.0     7.0    NaN
    2     NaN      NaN     2.0    3.0
    
    df['Fruit Total']=df.iloc[:,-4:].sum(axis=1)
    
    print(df)
       Apples  Bananas  Grapes  Kiwis  Fruit Total
    0     2.0      3.0     NaN    1.0          6.0
    1     1.0      3.0     7.0    NaN         11.0
    2     NaN      NaN     2.0    3.0          5.0
    

    【讨论】:

    • 感谢您的回答。但是,我不明白在 iloc 语句中使用负号有什么好处。 iloc[:,1,5] 似乎是一种更简单且不易混淆的方式。我正在学习 Python 和 Pandas。通过反复试验,我意识到 iloc[1:4] 只是对前 3 列求和,而 iloc[:,1,5] 对前 4 列求和
    • 使用 iloc[:,-4] 你告诉它最后 4 列。在这种情况下,iloc[:,-4] = iloc[:,1,5]。您使用哪一种取决于您希望在陈述中体现的具体程度或开放程度。
    【解决方案5】:

    如果您想在不知道数据框的形状/大小的情况下得出总数,我想以 Ramon 的回答为基础。 我将在下面使用他的答案,但修复一个不包括总数最后一列的项目。 我已经从形状中删除了 -1:

    cols_to_sum = df.columns[ : df.shape[1]-1]
    

    到这里:

    cols_to_sum = df.columns[ : df.shape[1]]
    
    print(df)
       Apples  Bananas  Grapes  Kiwis
    0     2.0      3.0     NaN    1.0
    1     1.0      3.0     7.0    NaN
    2     NaN      NaN     2.0    3.0
    
    cols_to_sum = df.columns[ : df.shape[1]]
    
    df['Fruit Total'] = df[cols_to_sum].sum(axis=1)
    
    print(df)
       Apples   Bananas Grapes  Kiwis   Fruit Total
    0  2.0      3.0     NaN     1.0     6.0
    1  1.0      3.0     7.0     NaN     11.0
    2  NaN      NaN     2.0     3.0     5.0
    

    这会在不跳过最后一列的情况下为您提供正确的总数。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-05-28
      • 1970-01-01
      • 2021-02-19
      • 1970-01-01
      • 2015-07-08
      相关资源
      最近更新 更多