简体   繁体   English

在python中对数据框中的列求和

[英]Summing columns in dataframe in python

I am trying to add 3 columns' values to come up with a new column as total value.我正在尝试添加 3 列的值以提出一个新列作为总值。 Code is below:代码如下:

    df3[["Bronze","Gold","Silver"]] = 
    df3[["Bronze","Gold","Silver"]].astype("int")
    df3["Total Medal"]= df3.iloc[:, -3:0].sum(axis=1)
    df3[["Total Medal"]].astype("int")

I know that Bronze, Gold, Silver columns have 1 and 0 values and they are the last 3 columns in the dataframe.我知道 Bronze、Gold、Silver 列有 1 和 0 值,它们是数据框中的最后 3 列。 Their original types were "uint8" so I changed them to "int".它们的原始类型是“uint8”,所以我将它们更改为“int”。

Total Medal column after these lines come out as type "float" (instead of int) and yield only the value 0. How can I properly add these columns?这些行后的 Total Medal 列作为“float”类型(而不是 int)出现并且仅产生值 0。如何正确添加这些列?

要将 3 列的值添加到新列中,只需执行

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

This can eg be done using assign :这可以例如使用assign来完成:

import numpy as np
import pandas as pd

#create data frame
data = {"gold":np.random.choice([0,1],size=10),"silver":np.random.choice([0,1],size=10), "bronze":np.random.choice([0,1],size=10)}
df = pd.DataFrame(data)

#calculate new column and add to dataframe
df = df.assign(mysum=df.gold+df.silver+df.bronze)

Edit: df["mysum"] = df.sum(axis=1) only works if your dataframe only has the three relevant columns, because it sums over all columns (and not only over the three you want).编辑: df["mysum"] = df.sum(axis=1)仅当您的数据df["mysum"] = df.sum(axis=1)只有三个相关列时才有效,因为它对所有列(而不仅仅是您想要的三列)求和。

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

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