如何使用 pandas 将 1 行数据拆分为多列

发布于 2025-01-13 05:23:29 字数 2214 浏览 3 评论 0原文

我目前正在处理 CAN 总线数据,将所有内容输出到 1 列、多行中。代码的工作方式是通过向汽车的 Can-bus 网络发送和接收消息来接收实时数据。接收到的数据存储在变量“response”中,打印出来时会给出以下数据。

Timestamp: 1646874440.563719        ID: 0060    S Rx                DL:  8    00 00 00 00 00 00 00 08     Channel: can0
Timestamp: 1646874440.563959        ID: 0090    S Rx                DL:  8    00 00 c2 00 61 83 00 00     Channel: can0
Timestamp: 1646874440.564195        ID: 00c0    S Rx                DL:  8    00 2d 61 27 50 00 39 f3     Channel: can0
Timestamp: 1646874440.565563        ID: 0010    S Rx                DL:  8    0d e7 00 00 00 00 81 47     Channel: can0
Timestamp: 1646874440.566430        ID: 00c8    S Rx                DL:  8    cb 87 3e f1 10 38 c6 02     Channel: can0
Timestamp: 1646874440.567062        ID: 0160    S Rx                DL:  8    55 ff 00 00 57 23 ed ff     Channel: can0
Timestamp: 1646874440.567308        ID: 0180    S Rx                DL:  8    03 00 0d f8 17 fe f0 00     Channel: can0
Timestamp: 1646874440.567558        ID: 0190    S Rx                DL:  8    80 00 80 00 80 00 80 00     Channel: can0
Timestamp: 1646874440.567802        ID: 01c0    S Rx                DL:  8    f7 c0 00 00 80 00 00 00     Channel: can0
Timestamp: 1646874440.568048        ID: 01d0    S Rx                DL:  8    a8 ff ff 00 00 00 00 00     Channel: can0
Timestamp: 1646874440.568282        ID: 01e0    S Rx                DL:  8    58 e1 de 05 00 00 fd 23     Channel: can0
Timestamp: 1646874440.568531        ID: 0210    S Rx                DL:  8    00 00 00 00 f2 9b 00 00     Channel: can0
Timestamp: 1646874440.568772        ID: 00a0    S Rx                DL:  8    01 7b 29 f3 00 00 7f fe     Channel: can0
Timestamp: 1646874440.569017        ID: 0213    S Rx                DL:  8    00 f2 00 00 0f ff df ff     Channel: can0

我正在尝试寻找一种方法将此数据分为单独的“时间戳”“ID”“DL”“通道”列以及每个十六进制值的单独列。我需要将它们分开,以便排序并获取不同显示数据(例如转速、转向角)所需的代码和值。该数据设置为每 1/10 秒更新一次,但它可以增加,我不知道是否应该建立一个系统在一段时间后删除旧数据,这样就不会填满数据贮存。

我对 python 索引和数据记录相当陌生,所以任何东西都会有帮助。

谢谢您的宝贵时间!

我目前正在测试的代码(评论中的说明)。

import pandas as pd

df = pd.read_table("CB.txt" , delimiter=" ")
df = df.str.split(" ", 1, expand = True)
print(df)

I am currently working with CAN-Bus data that outputs everything into 1 column, multiple rows. The way the code works is by sending and receiving msg to a car's Can-bus network to receive real-time data. The received data is stored in a variable "response" which when printed out, gives the following data.

Timestamp: 1646874440.563719        ID: 0060    S Rx                DL:  8    00 00 00 00 00 00 00 08     Channel: can0
Timestamp: 1646874440.563959        ID: 0090    S Rx                DL:  8    00 00 c2 00 61 83 00 00     Channel: can0
Timestamp: 1646874440.564195        ID: 00c0    S Rx                DL:  8    00 2d 61 27 50 00 39 f3     Channel: can0
Timestamp: 1646874440.565563        ID: 0010    S Rx                DL:  8    0d e7 00 00 00 00 81 47     Channel: can0
Timestamp: 1646874440.566430        ID: 00c8    S Rx                DL:  8    cb 87 3e f1 10 38 c6 02     Channel: can0
Timestamp: 1646874440.567062        ID: 0160    S Rx                DL:  8    55 ff 00 00 57 23 ed ff     Channel: can0
Timestamp: 1646874440.567308        ID: 0180    S Rx                DL:  8    03 00 0d f8 17 fe f0 00     Channel: can0
Timestamp: 1646874440.567558        ID: 0190    S Rx                DL:  8    80 00 80 00 80 00 80 00     Channel: can0
Timestamp: 1646874440.567802        ID: 01c0    S Rx                DL:  8    f7 c0 00 00 80 00 00 00     Channel: can0
Timestamp: 1646874440.568048        ID: 01d0    S Rx                DL:  8    a8 ff ff 00 00 00 00 00     Channel: can0
Timestamp: 1646874440.568282        ID: 01e0    S Rx                DL:  8    58 e1 de 05 00 00 fd 23     Channel: can0
Timestamp: 1646874440.568531        ID: 0210    S Rx                DL:  8    00 00 00 00 f2 9b 00 00     Channel: can0
Timestamp: 1646874440.568772        ID: 00a0    S Rx                DL:  8    01 7b 29 f3 00 00 7f fe     Channel: can0
Timestamp: 1646874440.569017        ID: 0213    S Rx                DL:  8    00 f2 00 00 0f ff df ff     Channel: can0

I am trying to look for a way to separate this data into separate "Timestamp" "ID" "DL" "Channel" columns and separate ones for each hex value. I need them separated in order to sort through and get the codes and values needed for different display data (Ex. RPM, Steering angle). This data is set to update every 1/10 of a second however it can increase, I don't know if a system should be put in place to deleted old data after a certain amount of time so it doesn't fill up the data storage.

I am rather new to python indices and data logging so anything would help.

Thank you for your time!

Code I am currently testing with (Clarifications in the comments).

import pandas as pd

df = pd.read_table("CB.txt" , delimiter=" ")
df = df.str.split(" ", 1, expand = True)
print(df)

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

成熟稳重的好男人 2025-01-20 05:23:30

好的,所以我最终找到了解决方案。最大的问题是空格算作表中的一个元素,因此您必须为空格创建一列,然后立即删除该列。

以下代码对我有用。

import pandas as pd

df = pd.read_table("CB.txt", sep=" ", header=None)
df.columns = ["a","Timestamp","c","d","e","f","g","h","i","j","ID","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","fag","9","10","11","B0","B1","B2","B3","B4","B5","B6","B7","20","21","22","23","24","Channel"]
df = df.drop(columns=['a','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','fag','7','9','10','11','20','21','22','23','24'])   

将出现错误,说明所需元素的数量,因此此列表将根据您的需要进行更改。这绝对不是最好的方法,但它仍然是一个解决方案。

OK, so I did end up finding a solution. The biggest problem is that the white spaces count as an element in a table so you have to make a column for the white space and deleted the column right after.

The following code worked for me.

import pandas as pd

df = pd.read_table("CB.txt", sep=" ", header=None)
df.columns = ["a","Timestamp","c","d","e","f","g","h","i","j","ID","l","m","n","o","p","q","r","s","t","u","v","w","x","y","z","1","2","3","4","5","6","7","fag","9","10","11","B0","B1","B2","B3","B4","B5","B6","B7","20","21","22","23","24","Channel"]
df = df.drop(columns=['a','c','d','e','f','g','h','i','j','l','m','n','o','p','q','r','s','t','u','v','w','x','y','z','1','2','3','4','5','6','fag','7','9','10','11','20','21','22','23','24'])   

An error will appear stating the number of elements needed, so this list will change according to what you need. This is definitely not the best way to do this, but its a solution non the less.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文