Reputation: 111
I have a Tags dataframe that has 2 columns "Date" & "Tag", there is multiple same dates and multiple of the same tag. I need to sort the table so that it shows a unique date alongside a unique tag and the count of how many times that tag occurred that month. Any ideas how I could do this? Please see below a screenshot of the current table.
Upvotes: 0
Views: 187
Reputation: 4482
import pandas as pd
df = pd.DataFrame({'Date': ['Nov','Nov','Dec'],
'Name':[1,2,1]}) # sample dataframe
If you want to have the count
of the unique Names
by Date
you can do this
df.groupby('Date')['Name'].nunique()
Date
Dec 1
Nov 2
If you want to add this information as a new column in your dataframe you can do this
df['count'] = df.groupby('Date')['Name'].transform('nunique')
Date Name count
0 Nov 1 2
1 Nov 2 2
2 Dec 1 1
Upvotes: 1