尝试为以下SQL查找等效的熊猫:
SELECT KnownSince, COUNT(1)
FROM mytable
GROUP BY KnownSince
我已经测试过:
aux.groupby(['KnownSince'])['KnownSince'].agg(['count']),
aux.groupby(['KnownSince']).agg(['count']),
aux['KnownSince'].groupby(['KnownSince']).agg(['count']),
aux['KnownSince'].groupby().agg(['count'])
但是没有达到扩展结果。
PS:KnownSince
是格式为YYYYMM的数字,而不是日期时间对象。
答案 0 :(得分:2)
它是size
:
df.groupby('KnownSince', as_index=False).size()
或命名为agg
:
df.groupby('KnownSince').agg(count=('KnownSince','count')).reset_index()
答案 1 :(得分:1)
在pandas
中,内置函数value_counts
df['KnownSince'].value_counts()