0

I am a beginner at python, so bear with me!

My dataset is from excel and I was curious how to find and add a frequency column for my ID.

I first performed the groupby function for ID and date by doing:

dfcount = dfxyz.groupby(["ID", "Date"])

and then found the mean by doing:

dfcount1 = dfcount.mean()

The output i got was:

Image

What I am trying to do is get the frequency number beside it like this:

Image2

I did not know how to copy python code, so I uploaded pictures! Sorry! Any help is appreciated for what code I can use to count the frequency for each ID AFTER I find the mean of the groupby columns.

Thank you in advance!

bla
  • 1,840
  • 1
  • 13
  • 17
  • 1
    The groupby method is cumcount, but this question needs to be improced with some runable code. The easiest way to get the code is to simply copy and paste (rather than take screenshots). – Andy Hayden Apr 19 '18 at 23:35
  • OK i figured it out, but how would I add the cumcount values as a colummn in my dataset. Or, how do I groupby the comcount as I want to find the mean for all the "1" value for cumcount, "2" value for cumcount, etc. – Mark White Apr 20 '18 at 00:02

2 Answers2

0

You can using groupby with cumcount

df['Freq']=(df.groupby(level=0).cumcount()+1).values
BENY
  • 317,841
  • 20
  • 164
  • 234
0

You can use this:

df['column_name'].value_counts()

value_counts - Returns object containing counts of unique values

Jimmys
  • 357
  • 1
  • 3
  • 14