0

I have a Pandas DataFrame like this :

df = pd.DataFrame({
    'Date': ['2017-1-1', '2017-1-1', '2017-1-2', '2017-1-2', '2017-1-3'],
    'Groups': ['one', 'one', 'one', 'two', 'two']})

    Date      Groups  
0  2017-1-1    one       
1  2017-1-1    one       
2  2017-1-2    one       
3  2017-1-2    two       
4  2017-1-3    two   

How can I generate a new DataFrame like this?

     Date    Groups_counts     
0  2017-1-1    1        
1  2017-1-2    2        
2  2017-1-3    1   

Thanks a lot!

ah bon
  • 9,293
  • 12
  • 65
  • 148
  • Do you mean grouping by how many unique values there are in the second column? I'm guessing you are, but you should explain that in your question. Also, what did you try? What output did you get? See if this helps: https://stackoverflow.com/questions/15411158/pandas-countdistinct-equivalent – Ofer Sadan May 24 '18 at 08:38

1 Answers1

2

To get count of unique records use:

df.groupby('Date')['Groups'].nunique()
zipa
  • 27,316
  • 6
  • 40
  • 58