Home > Back-end >  How to do a COUNTIFS in Pandas
How to do a COUNTIFS in Pandas

Time:10-27

Is there a way to add a column to a pandas dataframe that replicates an excel COUNTIFS formula to count the number of rows that meet multiple criteria? I currently have a basic 2 column dataframe with columns HOUR and SYMBOL. I want a 3rd column that runs the length of the dataframe and counts the number of times a HOUR SYMBOL pair appear. I have added a picture of the dataframe so far and here I would be expecting another column with values 1 1 2 2 1

as '08' & 'AD NA' are the only pair to appear twice.

enter image description here

CodePudding user response:

Try with groupby and transform

df['new'] = df.groupby(['HOUR','SYMBOL'])['HOUR'].transform('count')
  • Related