我想创建一个频率为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
谢谢
答案 0 :(得分:2)
这是我的建议,希望对您有帮助:
构建一些示例数据:
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]')
答案 1 :(得分:0)
我看不到使用矢量化函数来完成此操作的方法,这意味着您必须使用通常可以避免的操作来完成此操作,请使用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