Home > Net >  Counting values with Pandas
Counting values with Pandas

Time:03-05

I am trying to count values that are greater than 0. Below you can see my data

df = pd.DataFrame({"id_n":["1","2","3","4","5"],
                               "Sales":[0,1,1,4,5],
                               "Sales1":[0,1,5,4,5],
                               "Sales2":[1,0,0,4,5]
                  })

Now I want to count rows that have values greater than 0, and put them into the separate column you can see pic below

enter image description here

So can anybody help me how to solve this?

CodePudding user response:

df['counttotal'] =(df.select_dtypes(exclude='object')>0).sum(1)

CodePudding user response:

df['counttotal'] = df[['Sales','Sales1','Sales2']].gt(0).sum(axis=1)
  • Related