简体   繁体   中英

how to sum one col with condition on other col

i like to sum "col1" to a new row only if name= to "A"

that is the 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

i like it to be like this so the last row is 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

Let us try 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

You could set the value using 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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