簡體   English   中英

如何將一個 col 與另一個 col 的條件相加

[英]how to sum one col with condition on other col

僅當 name= 為“A”時,我才喜歡將“col1”與新行相加

那是df

data={"col1":[2,3,4,5,7],
"col2":[4,2,4,6,4],
"col3":[7,6,9,11,2],
"col4":[14,11,22,8,5],
"name":["A","A","V","A","B"],
"n_roll":[8,2,1,3,9]}
df=pd.DataFrame.from_dict(data)
df

我喜歡這樣,所以最后一行是 2+3+5=10

data={"col1":[2,3,4,5,7,10],
"col2":[4,2,4,6,4,0],
"col3":[7,6,9,11,2,0],
"col4":[14,11,22,8,5,0],
"name":["A","A","V","A","B",0],
"n_roll":[8,2,1,3,9,0]}
df=pd.DataFrame.from_dict(data)
df

讓我們嘗試append

out = df.append(df.loc[df.name=='A',['col1']].sum().to_frame().T,sort=True)
   col1  col2  col3  col4  n_roll name
0     2   4.0   7.0  14.0     8.0    A
1     3   2.0   6.0  11.0     2.0    A
2     4   4.0   9.0  22.0     1.0    V
3     5   6.0  11.0   8.0     3.0    A
4     7   4.0   2.0   5.0     9.0    B
0    10   NaN   NaN   NaN     NaN  NaN

您可以使用loc設置值:

df.loc[len(df), "col1"] = df.loc[df["name"] == "A", "col1"].sum()
df.fillna(0)


   col1    col2 col3    col4    name    n_roll
0   2.0     4.0 7.0     14.0    A       8.0
1   3.0     2.0 6.0     11.0    A       2.0
2   4.0     4.0 9.0     22.0    V       1.0
3   5.0     6.0 11.0    8.0     A       3.0
4   7.0     4.0 2.0     5.0     B       9.0
5   10.0    0.0 0.0     0.0     0       0.0

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM