Reputation: 53
ie)
count
2015-01 2
2015-02 1
2015-03 4
for the group i tried pd.groupby(b,by=[b.index.month,b.index.year])
but there was object has no attribute 'month' error
Upvotes: 1
Views: 92
Reputation: 294258
Apply sorted
with the key
parameter set to pd.to_datetime
df.assign(date=df.date.apply(sorted, key=pd.to_datetime))
id date
0 a [2015-02-01, 2015-03-01]
1 b [2015-03-01]
2 s [2015-01-01, 2015-03-01]
3 f [2015-01-01, 2015-01-01, 2015-03-01]
Then use pd.value_counts
pd.value_counts(pd.to_datetime(df.date.sum()).strftime('%Y-%m'))
2015-03 4
2015-01 3
2015-02 1
dtype: int64
debugging
You should be able to copy and paste this code... please verify that it runs as expected.
import pandas as pd
df = pd.DataFrame(dict(
id=list('absf'),
date=[
['2015-03-01', '2015-02-01'],
['2015-03-01'],
['2015-01-01', '2015-03-01'],
['2015-01-01', '2015-01-01', '2015-03-01']
]
))[['id', 'date']]
print(df.assign(date=df.date.apply(sorted, key=pd.to_datetime)))
print()
print(pd.value_counts(pd.to_datetime(df.date.sum()).strftime('%Y-%m')))
You should expect to see
id date
0 a [2015-02-01, 2015-03-01]
1 b [2015-03-01]
2 s [2015-01-01, 2015-03-01]
3 f [2015-01-01, 2015-01-01, 2015-03-01]
2015-03 4
2015-01 3
2015-02 1
dtype: int64
Upvotes: 1