簡體   English   中英

將格式為字典的 csv 文件讀入 pandas

[英]Reading csv file formatted as dictioary into pandas

我有一個包含傳感器數據的 csv 文件,其中一行的格式如下

1616580317.0733, {'Roll': 0.563820598084682, 'Pitch': 0.29817540218781163, 'Yaw': 60.18415650363684, 'gyroX': 0.006687641609460116, 'gyroY': -0.012394784949719908, 'gyroZ': -0.0027120113372802734, 'accX': -0.12778355181217196, 'accY': 0.24647256731987, 'accZ': 9.763526916503906}

其中第一列是時間戳,其余列是像 object 這樣的字典,其中包含各種測量量。

我想將其讀入 pandas 數組,其中包含["Timestamp","Roll","Pitch","Yaw","gyroX","gyroY","gyroZ","accX","accY","accZ"] 這樣做的有效方法是什么? 該文件為 600MB,因此需要解析的行數並不多。

我不確定你從哪里得到秒列。

下面的代碼將每一行解析為時間戳和字典。 然后將時間戳添加到字典中,最終將成為 dataframe 中的一行。

import json
import pandas as pd


def read_file(filename):
    

    chunk_size = 20000
    entries = []
    counter = 0
    
    df = pd.DataFrame()

    with open(filename, "r") as fh:
        for line in fh:
            timestamp, data_dict = line.split(",", 1)
            data_dict = json.loads(data_dict.replace("'", '"'))
            data_dict["timestamp"] = float(timestamp)
            entries.append(data_dict)
            counter += 1
            
            if counter == chunk_size:
                df = df.append(entries, ignore_index=True)
                entries = []
                counter = 0
                
        if counter != 0:
            df = df.append(entries, ignore_index=True)

                
    return df

read_file("sample.txt")

I think you should convert your csv file to json format and then look at this site on how to transform the dictionary into a pandas dataframe: https://www.delftstack.com/fr/howto/python-pandas/how-to-將python-dictionary-to-pandas-dataframe/#:~:text=2%20banana%2012-,M%C3%A9thode%20pandas.,le%20nom%20de%20la%20colonne

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM