我有一个数据框,我计划用它来构建直方图。
数据框包含以下值。
starttime hour
1 7/01/2015 0
2 7/01/2015 0
3 7/01/2015 3
4 7/01/2015 3
5 7/01/2015 12
我希望得到结果数据框。
starttime hour frequency
1 7/01/2015 0 2
2 7/01/2015 3 2
3 7/01/2015 12 1
到目前为止我做了什么
df_values = Df[['starttime','hour']]
values = df_values.groupby(['starttime'])
grouped = values.aggregate(np.sum)
输出我正在
hour
starttime
6/01/2015 0000000000000000000000000000000000000000000000...
6/02/2015 0000000000000000000000000000000000000000000000...
6/03/2015 0000000000000000000000000000000000000000000000...
6/04/2015 NaN
6/05/2015 435211
非常感谢任何帮助。感谢。
答案 0 :(得分:2)
使用groupby
+ size
/ count
-
c = df.columns.tolist() # c = ['starttime', 'hour']
df.groupby(c).size().reset_index(name='frequency')
或者,
df.groupby(c).hour.count().reset_index(name='frequency')
starttime hour frequency
0 7/01/2015 0 2
1 7/01/2015 3 2
2 7/01/2015 12 1
答案 1 :(得分:0)
df['freq'] = 1
df.groupby(['starttime','hour', as_index=False]).count()