2

I have data like as below:

df = pd.DataFrame({'date':['2020.2.1','2020.2.1','2020.2.1','2020.2.2','2020.2.1'],
                'fips':[4,5,4,5,5]})

and I want to get a result counting the frequency of fips each day like as below, but I don't want to ignore the overlapped index column (desired output):

pd.DataFrame({'date':['2020.2.1','2020.2.1','2020.2.2'],
                'fips':[4,5,5], 'count':[2,2,1]})

I tried df.groupby(['date','fips']).size() But it ignore the index value at second row and first column '2020.2.1'. Thanks in advance for your help..

8
  • 3
    Have you tried df[['date', 'fips']].value_counts() ? Commented Oct 11, 2020 at 3:32
  • Yeah, I did. but it gives error saying 'DataFrame' object has no attribute 'value_counts' Commented Oct 11, 2020 at 3:35
  • Which version of pandas do you have? I tried it worked for me Commented Oct 11, 2020 at 3:35
  • In the meantime, this should work too: df.groupby(['date', 'fips']).fips.agg(fips='first', freq=len) Commented Oct 11, 2020 at 3:38
  • Thank you for your help. but what I am trying to do is not to ignore the second row index column which is '2020.2.1' for second row. I want it visible twice in the dataframe. but only show only once.. Commented Oct 11, 2020 at 3:52

1 Answer 1

1

Just reset the index to make the first column 'fully visible'

df2 = df.groupby(['date','fips'])['fips'].agg(['count']).reset_index()
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.