Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Count identical dates in pandas dataframe [duplicate]

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'])
like image 682
Niccola Tartaglia Avatar asked Jan 02 '23 03:01

Niccola Tartaglia


1 Answers

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
like image 113
sacuL Avatar answered Jan 05 '23 17:01

sacuL