0

I have a df below as:

Day 
Morning
Day
Night 
Night
Day
Morning
Day
Day 

There are other columns in the this df and does not only contain the above column

when I run code below as:

df.groupby('day').count()

it outputs a df with the count of times each value for day column occurs in the other columns in the df ( its the same count number for each value in the day column)

How can I create a new column that lists those same counts for each value of the Day column?

Expected output:

 Day     New_Col
Morning    2
Day        4
Night      2
Night      2
Day        4
Morning    2
Day        4
Day        4

Thanks!

Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58
Chris90
  • 1,868
  • 5
  • 20
  • 42

2 Answers2

1

Use transform() if you want to map back:

df['New_Col'] = df.groupby('Day')['Day'].transform('count')

Or you can use map, and also, value_counts():

df['New_Col'] = df['Day'].map(df['Day'].value_counts())

Output:

       Day  New_Col
0  Morning        2
1      Day        4
2    Night        2
3    Night        2
4      Day        4
5  Morning        2
6      Day        4
7      Day        4
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74
1

Use df.groupby.transform('size'):

df['New_Col'] = df.groupby('Day')['Day'].transform('size')
Mayank Porwal
  • 33,470
  • 8
  • 37
  • 58