我目前正在学习python环境来处理传感器数据。
我有一块板,上面有32个传感器,读数是温度。在以下链接中,您可以找到原始数据的摘录:https://5e86ea3db5a86.htmlsave.net/
我正在尝试使用pandas从CSV文件中构建一个按日期分组的数据框(请参见表https://docs.google.com/spreadsheets/d/1zpDI7tp4nSn8-Hm3T_xd4Xz7MV6VDGcWGxwNO-8S0-s/edit?usp=sharing的潜在结构
到目前为止,我已经读取了pandas中的数据文件并删除了所有未命名的列。我正在努力创建一个柱传感器ID,它应该包含32个传感器ID和柱温度。
我应该如何遍历此CSV文件以创建3列(日期、传感器ID和温度)?
谢谢你的帮助
发布于 2020-04-20 23:52:50
看起来每一行的第一项是日期,然后是传感器id和值对,然后是我们可以排除的空值。如果是这样,那么下面的方法应该是可行的。如果没有,请尝试根据您的目的修改代码。
data = []
with open('filename.txt', 'r') as f:
for line in f:
# the if excludes empty strings
parts = [part for part in line.split(',') if part]
# this gets the date in a format that pandas can recognize
# you can omit the replace operations if not needed
sensor_date = parts[0].strip().replace('[', '').replace(']', '')
# the rest of the list are the parings of sensor and reading
sensor_readings = parts[1:]
# this uses list slicing to iterate over even and odd elements in list
# ::2 means every second item starting with zero, which are evens
# 1::2 means every second item starting with one, which are odds
for sensor, reading in zip(sensor_readings[::2], sensor_readings[1::2]):
data.append({'sensor_date': sensor_date,
'sensor': sensor,
'reading': reading})
pd.DataFrame(data)使用您的示例数据,我得到了以下结果:
=== Output: ===
Out[64]:
sensor_date sensor reading
0 Tue Jul 02 16:35:22.782 2019 28C037080B000089 16.8750
1 Tue Jul 02 16:35:22.782 2019 284846080B000062 17.0000
2 Tue Jul 02 16:35:22.782 2019 28A4BA070B00002B 16.8750
3 Tue Jul 02 16:35:22.782 2019 28D4E3070B0000D5 16.9375
4 Tue Jul 02 16:35:22.782 2019 28A21E080B00002F 17.0000
.. ... ... ...https://stackoverflow.com/questions/61324153
复制相似问题