Python(Pandas):给定时间值之间的记录计数(正在传输的数据包)

2024-06-09 13:33:02 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个基于来自无线信道的数据包日志的数据帧,它的索引"transmitted""received"分别是自启动以来的时间浮动。你知道吗

我想得到在任何时候空气中有多少包的分布。我现在是通过

t_packets = pd.Series([df[(df.transmitted < t) & (t < df.received)].count().max() for t in range(tmax)])

这在计算上是一个麻烦的问题(每次迭代大约40秒),但是我还没有用我有限的经验找到更好的方法。你知道吗

有没有人想出更好的方法来完成这个任务?你知道吗


Tags: 数据方法dfcount时间数据包maxseries
1条回答
网友
1楼 · 发布于 2024-06-09 13:33:02

基本上,我认为您需要将数据展平并从中创建一个时间序列。你知道吗

tx = pd.DataFrame(index=df.transmitted)
rx = pd.DataFrame(index=df.received)
tx['p'] = 1   #adding a packet
rx['p'] = -1  #receiving a packet

#create the time series here
t = pd.concat([tx, rx])
t.sort().cumsum()

从这里很容易算出实际时间。您也可以重新采样以使其更清晰,但这应该可以做到。你知道吗


编辑:添加如何按秒重采样:

#assuming that indexed times are in seconds
t.sort(inplace=True)
t.index = pd.to_datetime(t.index * 10e9) #to convert from nanoseconds to seconds
t.resample('s', how='sum').cumsum()

相关问题 更多 >