将can消息对象转换为pandas数据帧

问题描述 投票:0回答:1
  1. 我的日志按以下格式存储在列表中:

log = [can.Message(timestamp = 1551734911.0096931,arbitration_id = 0x14ff0065,extended_id = True,channel = 2,dlc = 8,data = [0xf4,0x7c,0x89,0x35,0x28,0xf,0xea,0xe]),可以。消息(timestamp = 1551734911.0102572,arbitration_id = 0x14ff0165,extended_id = True,channel = 2,dlc = 8,data = [0x40,0x14,0x0,0x36,0xd0,0x39,0x60,0x22]),can.Message(timestamp = 1551734911.0108252 ,arbitration_id = 0x14ff0265,extended_id = True,channel = 2,dlc = 8,data = [0x80,0x35,0x9,0xf,0x8c,0x0,0x0,0x0]),can.Message(timestamp = 1551734911.0114133,arbitration_id = 0x14fef100, extended_id = True,channel = 2,dlc = 8,data = [0xff,0xff,0xff,0xff,0xff,0xff,0xff,0xff])]

目标:将日志转换为如下所示的pandas数据框:

timestamp   arbitration_id  extended_id channel dlc data

0 1551734911.00969 0x14ff0065 TRUE 2 8 [0xf4,0x7c,0x89,0x35,0x28,0xf,0xea,0xe] 1 1551734911.01025 = 0x14ff0165 TRUE 2 8 [0x40,0x14,0x0,0x36,0xd0,0x39,0x60,0x22] 2 1551734911.01082 0x14ff0265 TRUE 2 8 [0x80,0x35,0x9,0xf,0x8c,0x0,0x0,0x0]

我尝试了以下代码:

log = can.BLFReader(filename)
log = list(log)
df = pd.DataFrame(log)
print(df.head(5))

输出:

                                               0

0时间戳:1551734911.009693 ID:14ff0065 ... 1时间戳:1551734911.010257 ID:14ff0165 ... 2时间戳:1551734911.010825 ID:14ff0265 ... 3时间戳:1551734911.011413 ID:14fef100 ... 4时间戳:1551734911.011973 ID:14ff0068 ...

python-3.x pandas can-bus
1个回答
0
投票
import pandas as pd
import can

print(pd.__version__)

log = [can.Message(timestamp=1551734911.0096931, arbitration_id=0x14ff0065, extended_id=True, channel=2, dlc=8, data=[0xf4, 0x7c, 0x89, 0x35, 0x28, 0xf, 0xea, 0xe]), can.Message(timestamp=1551734911.0102572, arbitration_id=0x14ff0165, extended_id=True, channel=2, dlc=8, data=[0x40, 0x14, 0x0, 0x36, 0xd0, 0x39, 0x60, 0x22]), can.Message(timestamp=1551734911.0108252, arbitration_id=0x14ff0265, extended_id=True, channel=2, dlc=8, data=[0x80, 0x35, 0x9, 0xf, 0x8c, 0x0, 0x0, 0x0]), can.Message(timestamp=1551734911.0114133, arbitration_id=0x14fef100, extended_id=True, channel=2, dlc=8, data=[0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff, 0xff])]

df_data = [{'timestamp':m.timestamp, 'arbitration_id':m.arbitration_id, 'extended_id':m.is_extended_id, 'channel':m.channel, 'dlc': m.dlc, 'data':m.data} for m in log]
df = pd.DataFrame(df_data)

© www.soinside.com 2019 - 2024. All rights reserved.