从二进制序列到时间序列频率

2024-04-20 02:27:17 发布

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

我有一个数据集,其中包含来自存在传感器的数据,存储为二进制序列,频率为1小时。 传感器每2分钟检查一次是否存在,并每小时以二进制顺序存储(0表示未检测到存在,1表示检测到存在),这意味着一个数据点有30位数字。 一个传感器一小时的数据点如下00111111110000001000000100

我想创建的数据帧有2分钟的频率和每个传感器,列表示存在或不存在(1或0)

你知道怎么做吗

MACAddress          f8:f0:05:d0:ee:29              f8:f0:05:d0:f1:0b                f8:f0:05:d0:f1:1d   
2019-04-02 09:00:00 100100000000000000000000000000 100000000000001111111111010111   111111110111111111111110110000
2019-04-02 09:00:00 001110110000000000000000011111 111110000000000000111111110100 111010110011111111111011111111

谢谢


Tags: 数据二进制序列数字传感器ee频率f1
2条回答

我看不到使用矢量化函数的方法,这意味着您必须使用通常要避免的方法来完成,即使用df.iterrows手动迭代行

import pandas as pd

def get_subtimes(df):
    previous = df.index[0] - (df.index[1] - df.index[0])
    for index, row in df.iterrows():
        yield pd.DataFrame(row.apply(lambda x: list(map(int, x))).to_dict(),
                           index=pd.date_range(previous, index, freq="2T")[1:])
        previous = index


df = pd.DataFrame([["100100000000000000000000000000", "100000000000001111111111010111", "111111110111111111111110110000"],
                   ["001110110000000000000000011111", "111110000000000000111111110100", "111010110011111111111011111111"]],
                  columns=["f8:f0:05:d0:ee:29", "f8:f0:05:d0:f1:0b", "f8:f0:05:d0:f1:1d"],
                  index=pd.to_datetime(["2019-04-02 09:00:00", "2019-04-02 10:00:00"]))

print(pd.concat(get_subtimes(df)))

结果:

                     f8:f0:05:d0:ee:29  f8:f0:05:d0:f1:0b  f8:f0:05:d0:f1:1d
2019-04-02 08:02:00                  1                  1                  1
2019-04-02 08:04:00                  0                  0                  1
2019-04-02 08:06:00                  0                  0                  1
2019-04-02 08:08:00                  1                  0                  1
2019-04-02 08:10:00                  0                  0                  1
2019-04-02 08:12:00                  0                  0                  1
2019-04-02 08:14:00                  0                  0                  1
2019-04-02 08:16:00                  0                  0                  1
2019-04-02 08:18:00                  0                  0                  0
2019-04-02 08:20:00                  0                  0                  1
2019-04-02 08:22:00                  0                  0                  1
2019-04-02 08:24:00                  0                  0                  1
2019-04-02 08:26:00                  0                  0                  1
2019-04-02 08:28:00                  0                  0                  1
2019-04-02 08:30:00                  0                  1                  1
2019-04-02 08:32:00                  0                  1                  1
2019-04-02 08:34:00                  0                  1                  1
2019-04-02 08:36:00                  0                  1                  1
2019-04-02 08:38:00                  0                  1                  1
2019-04-02 08:40:00                  0                  1                  1
2019-04-02 08:42:00                  0                  1                  1
2019-04-02 08:44:00                  0                  1                  1
2019-04-02 08:46:00                  0                  1                  1
2019-04-02 08:48:00                  0                  1                  0
2019-04-02 08:50:00                  0                  0                  1
2019-04-02 08:52:00                  0                  1                  1
2019-04-02 08:54:00                  0                  0                  0
2019-04-02 08:56:00                  0                  1                  0
2019-04-02 08:58:00                  0                  1                  0
2019-04-02 09:00:00                  0                  1                  0
2019-04-02 09:02:00                  0                  1                  1
2019-04-02 09:04:00                  0                  1                  1
2019-04-02 09:06:00                  1                  1                  1
2019-04-02 09:08:00                  1                  1                  0
2019-04-02 09:10:00                  1                  1                  1
2019-04-02 09:12:00                  0                  0                  0
2019-04-02 09:14:00                  1                  0                  1
2019-04-02 09:16:00                  1                  0                  1
2019-04-02 09:18:00                  0                  0                  0
2019-04-02 09:20:00                  0                  0                  0
2019-04-02 09:22:00                  0                  0                  1
2019-04-02 09:24:00                  0                  0                  1
2019-04-02 09:26:00                  0                  0                  1
2019-04-02 09:28:00                  0                  0                  1
2019-04-02 09:30:00                  0                  0                  1
2019-04-02 09:32:00                  0                  0                  1
2019-04-02 09:34:00                  0                  0                  1
2019-04-02 09:36:00                  0                  0                  1
2019-04-02 09:38:00                  0                  1                  1
2019-04-02 09:40:00                  0                  1                  1
2019-04-02 09:42:00                  0                  1                  1
2019-04-02 09:44:00                  0                  1                  0
2019-04-02 09:46:00                  0                  1                  1
2019-04-02 09:48:00                  0                  1                  1
2019-04-02 09:50:00                  0                  1                  1
2019-04-02 09:52:00                  1                  1                  1
2019-04-02 09:54:00                  1                  0                  1
2019-04-02 09:56:00                  1                  1                  1
2019-04-02 09:58:00                  1                  0                  1
2019-04-02 10:00:00                  1                  0                  1

这是我的建议,希望能有所帮助:

构建一些示例数据:

dd = {'f8:f0:05:d0:ee:29': ['100100000000000000000000000000', '001110110000000000000000011111'],
      'f8:f0:05:d0:f1:0b': ['100000000000001111111111010111', '111110000000000000111111110100'],
      'f8:f0:05:d0:f1:1d': ['111111110111111111111110110000', '001110110000000000000000011111']}
df = pd.DataFrame(dd, index=['2019-04-02 09:00:00', '2019-04-02 10:00:00'])
df.index = pd.to_datetime(df.index)

解决方案:

expanded = df.apply(lambda series: series.apply(lambda x: pd.Series(list(x))).stack())
expanded.index = expanded.index.get_level_values(0) + (expanded.index.get_level_values(1)*2).astype('timedelta64[m]')

相关问题 更多 >