我有以下熊猫df(datetime属于类型datetime64):
device datetime
0 846ee 2020-03-22 14:27:29
1 0a26e 2020-03-22 15:33:31
2 8a906 2020-03-27 16:19:06
3 6bf11 2020-03-27 16:05:20
4 d3923 2020-03-23 18:58:51
我想使用 Seaborn 的 KDE 功能distplot。尽管我不完全明白为什么,但我还是让它工作了:
df['hour'] = df['datetime'].dt.floor('T').dt.time
df['hour'] = pd.to_timedelta(df['hour'].astype(str)) / pd.Timedelta(hours=1)
进而
sns.distplot(df['hour'], hist=False, bins=arr, label='tef')
问题是:我如何做同样的事情,但只计算 unique devices?我努力了
df.groupby(['hour']).nunique().reset_index()
df.groupby(['hour'])[['device']].size().reset_index()
但是他们给了我不同的结果(数量级相同,但或多或少)。我想我不明白我在做什么pd.to_timedelta(df['hour'].astype(str)) / pd.Timedelta(hours=1)
,这让我无法思考独特之处……也许吧。
30秒到达战场
婷婷同学_
相关分类