how to sum rows with condition? (pandas)

matan

i have this df

data={"col1":["n",3,"n",5,"n",2,6],
"col2":[4,"n",4,6,"n","n",5],
"col3":[7,"n",0,"n","n",6,7],
"col4":[14,11,22,8,6,"n",9],
"col5":[0,5,"n",3,8,"n",9],
"type":["B","n","n","n","B","A","B"],
"number":["one","n","n","n","one","two","n"]}
df=pd.DataFrame.from_dict(data)
print(df)

i like to have new column that sum all "n" in evry row/ Something like this maybe:

df["sum_n"]=df[["list of all col"]].sum("n",axis=)

its need to look like this

data={"col1":["n",3,"n",5,"n",2,6],
"col2":[4,"n",4,6,"n","n",5],
"col3":[7,"n",0,"n","n",6,7],
"col4":[14,11,22,8,6,"n",9],
"col5":[0,5,"n",3,8,"n",9],
"type":["B","n","n","n","B","A","B"],
"number":["one","n","n","n","one","two","n"],
"sum_n":[1,4,4,3,3,3,1]}
df=pd.DataFrame.from_dict(data)
df
Dani Mesejo

Do:

df['sum_n'] = df.eq('n').sum(1)
print(df)

Output

  col1 col2 col3 col4 col5 type number  sum_n
0    n    4    7   14    0    B    one      1
1    3    n    n   11    5    n      n      4
2    n    4    0   22    n    n      n      4
3    5    6    n    8    3    n      n      3
4    n    n    n    6    8    B    one      3
5    2    n    6    n    n    A    two      3
6    6    5    7    9    9    B      n      1

Collected from the Internet

Please contact [email protected] to delete if infringement.

edited at
0

Comments

0 comments
Login to comment

Related

Pandas: sum every N amount of rows by condition

Pandas: sum consecutive rows satisfying condition

Pandas sum rows by group based on condition

How to create condition on pandas rows

Pandas: How to sum particular rows

How to merge multiple rows by a given condition and sum?

Sum few rows with condition on basis of other column in Pandas

Apply multiple condition groupby + sort + sum to pandas dataframe rows

Pandas: Selecting rows for which groupby.sum() satisfies condition

Pandas dataframe - count the sum of rows up to the condition and compare it with the list elements

Pandas Dataframe - record number of rows based on cumulative sum on a column with a condition

sum rows with condition and groupby

Performing a sum on rows with condition

How to sum values in pandas based on bool condition

how to select rows based on two condition pandas

How to get index number of rows with condition in pandas

how to get rows satisfying certain condition pandas

How could i drop rows in Pandas in condition?

How to count rows where condition is false Pandas?

Pandas: How to sum values in a column for duplicate rows

How to sum values of pandas dataframe by rows?

How to sum distinct rows in a pandas Dataframe

How to sum values of particular rows in pandas?

How to sum rows containing specific targets in pandas?

How to sum the rows by using the name of multiindex in pandas?

How to get first rows of each condition and sum specific fields in Mongodb

How to get a sum of all rows that meets condition in postgres

How do I use SQL window to sum rows with a condition

How to sum two rows by a simple condition in a data frame