我已经将我的数据帧索引到DateTimeIndex,如下所示:
timstamp _id
2018-05-09 16:56:40.940 somedata1
2018-05-09 16:54:03.959 somedata2
2018-05-10 16:53:42.975 somedata3
2018-05-11 16:52:44.897 somedata4
2018-05-11 16:46:35.902 somedata5
我想表明每个日期的频率如下:
day count
2018-05-09 2
2018-05-10 1
2018-05-11 2
提前致谢!
答案 0 :(得分:1)
这是一种方式。
# convert to datetime
df['timestamp'] = pd.to_datetime(df['timestamp'])
# normalize, count values, convert to dataframe
res = df['timestamp'].dt.normalize()\
.value_counts()\
.to_frame().reset_index()
# rename columns
res.columns = ['timestamp', 'count']
结果:
print(res)
timestamp count
0 2018-05-09 2
1 2018-05-11 2
2 2018-05-10 1
答案 1 :(得分:1)
str.split
+ groupby
+ count
df.groupby(df['timstamp'].str.split().str[0])._id.count().reset_index()
timstamp _id
0 2018-05-09 2
1 2018-05-10 1
2 2018-05-11 2
to_datetime
+ groupby
+ count
df.assign(
timstamp=pd.to_datetime(df['timstamp']).dt.floor('D')
).groupby('timstamp', as_index=False)._id.count()
或者,
df['timstamp'] = pd.to_datetime(df['timstamp']).dt.floor('D')
df.groupby('timstamp', as_index=False)._id.count()
timstamp _id
0 2018-05-09 2
1 2018-05-10 1
2 2018-05-11 2