2

New to python and I am trying to replicate excel COUNTIFS formula using pandas. COUNTIFS(A:A,A2,B:B,B2,C:C,C2)

excel screenshot

The countifs column in above screenshot shows my expected result.

theduck
  • 2,589
  • 13
  • 17
  • 23
Suji
  • 87
  • 2
  • 8

1 Answers1

3

We have groupby + transform

df['countif']=df.groupby(['Col1','Col2','Col3'])['Col3'].transform('count')
BENY
  • 317,841
  • 20
  • 164
  • 234
  • Best solution! I only could have thought of a work around as I didn´t know ```transform``` yet. Thank you for posting this! – mgruber Nov 04 '19 at 15:15
  • Thanks it worked. Is it possible to give a brief explanation on how it works. – Suji Nov 04 '19 at 16:12
  • @Suji https://stackoverflow.com/questions/27517425/apply-vs-transform-on-a-group-object – BENY Nov 04 '19 at 16:14