I have a dataframe with a date column and I would like to create a new column that tells me how many identical dates the dataset contains. This is a min example of the original data set:
df1:
date
2017/01/03
2017/01/03
2017/01/04
2017/01/04
2017/01/04
2017/01/05
I would like to create this date_count, so the target data set is:
df1:
date date_count
2017/01/03 2
2017/01/03 2
2017/01/04 3
2017/01/04 3
2017/01/04 3
2017/01/05 1
The actual code to create df1:
dict1 = [{'date': '2017/01/03', 'date_count': 2},{'date': '2017/01/03', 'date_count': 2},
{'date': '2017/01/04', 'date_count': 3},{'date': '2017/01/04', 'date_count': 3},
{'date': '2017/01/04', 'date_count': 3},{'date': '2017/01/05', 'date_count': 1}]
df = pd.DataFrame(dict1, index=['s1', 's2','s3','s1','s2','s3'])
Here is another method using map
along with a groupby
and size
:
>>> df
date
s1 2017/01/03
s2 2017/01/03
s3 2017/01/04
s1 2017/01/04
s2 2017/01/04
s3 2017/01/05
df['date_count'] = df.date.map(df.groupby('date').size())
>>> df
date date_count
s1 2017/01/03 2
s2 2017/01/03 2
s3 2017/01/04 3
s1 2017/01/04 3
s2 2017/01/04 3
s3 2017/01/05 1
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With