有750k行exclude
,其中有15列,还有一个df
称为pd.Timestamp
的{{1}}。
我几乎实时地将实时数据处理到毫秒。
现在,我想将来自index
中较高时间分辨率的一些统计数据作为新列应用于大ts
。 df_stats
的时间分辨率为1分钟。
df
df_stats
当前我有下面的代码,但是它效率低下,因为它需要遍历完整的数据。
我想知道是否有使用$ df
+----------------+---+---------+
| ts | A | new_col |
+----------------+---+---------+
| 11:33:11.31234 | 1 | 81 |
+----------------+---+---------+
| 11:33:11.64257 | 2 | 81 |
+----------------+---+---------+
| 11:34:10.12345 | 3 | 60 |
+----------------+---+---------+
,$ df_stats
+----------------+----------------+
| ts | new_col_source |
+----------------+----------------+
| 11:33:00.00000 | 81 |
+----------------+----------------+
| 11:34:00.00000 | 60 |
+----------------+----------------+
或pd.cut
的简单解决方案?还是用其他方法将两个索引上的时间桶合并?
bin
答案 0 :(得分:2)
让我们尝试一些新的reindex
df_stats=df_stats.set_index('ts').reindex(df['ts'], method='nearest')
df_stats.index=df.index
df=pd.concat([df,df_stats],axis=1)
或
df=pd.merge_asof(df, df_stats, on='ts',direction='nearest')