如何读取csv文件中包含的python dict并将数据存储在数据帧中?

2024-06-06 22:16:11 发布

您现在位置:Python中文网/ 问答频道 /正文

我有一个csv,其中每一行是一个字典,在每一行的dict中,有一个列表,这个列表包含一个子列表和一个子列表。每个子列表有2个元素,子列表有100个键,每个键有一个值。这是数据截图:

enter image description here

以下是文本格式的数据示例:

{"0": [[10.8, 36.0], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0}]}
{"1": [[10.8, 36.1], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0}]}
{"2": [[10.8, 36.2], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0}]}
{"3": [[10.8, 36.300000000000004], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0}]}
{"4": [[10.8, 36.4], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0}]}
{"5": [[10.8, 36.5], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0}]}
{"6": [[10.8, 36.6], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0}]}
{"7": [[10.8, 36.7], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0, "10": 0}]}
{"8": [[10.8, 36.800000000000004], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0}]}
{"9": [[10.8, 36.9], {"0": 0, "1": 0, "2": 0, "3": 0, "4": 0, "5": 0, "6": 0, "7": 0, "8": 0, "9": 0}]}

我想做的是将其读入一个pandas数据帧,该数据帧将产生这样的预期输出(为了简单起见,我只键入一行):

list_elemnt_1   list_elemnt_2  key_0,  key_1,  key_2,  key_3,  key_4,  and so on...
        value           value  value   value   value   value   value   and so on...

对于csv中的每一行,我想构建一个dataframe,每个子列表值(2)有一列,该行dict中包含的子目录中的每个键有一列

我怎么能这么做??如果需要,请随时询问更多信息。你知道吗

事先非常感谢

编辑

按键0、按键1、按键2等。。。是子目录键,不是主目录键


Tags: andcsv数据key元素列表字典so
2条回答

不是最好的办法。你知道吗

# Edit for reading the csv

# there are two ways to go about it, I am assuming data is in 1 column
df_csv = pd.read_csv('/path/to/your/file/filename.csv')


# read in the csv, I assume you are able to do this.
list_of_dfs = []
for idx, row in df_csv.iterrows():
      d = row[column_name]   # find the column name and insert here
      df = pd.DataFrame.from_dict(d,orient='index') # creating a dataframe to get the number of lines

     remove_cols = df.columns

    for i in d.keys():
         df['list_elemnt_1'] = d[i][0][0]
         df['list_elemnt_2'] = d[i][0][1]
         for key in d[i][1].keys():
               df[key] = d[i][1][key]

         # remove the original cols here
         list_of_dfs.append(df)


这将为您提供每行的df,作为list_of_dfs中的一个行元素,我假设这是目标? 让我知道它是否有效。你知道吗

import ast
import pandas as pd

file = open('file_55966371.csv', 'r')

lines = [ast.literal_eval(line) for line in file]

def clean_lines(line):
    value = [v for v in line.values()]

    l1, l2 = value[0][0]

    line_dict = value[0][1]

    line_dict = {f'key_{key}': value for key, value in line_dict.items()}

    line_dict['list_element1'] = l1
    line_dict['list_element2'] = l2

    return line_dict

to_read = [clean_lines(line) for line in lines]

df = pd.DataFrame(to_read)

我同意@furas这看起来很像一个JSON,如果这个数据是从某人那里得到的,你最好问问他们是否可以用JSON格式发送给你。你知道吗

如果没有,上面的代码就起作用了。你知道吗

  • 打开文件。

  • 阅读每一行并将其存储为一个列表。ast.literal_eval允许Python从一开始就识别出它是一个字典,并将它们存储为dict对象。

  • 我创建了一个helperclean_lines函数。干净的线是更重要的部分。你知道吗

    1. 获取值(即带有子列表和子列表的列表)
    2. 将列表解压为两个变量l1l2
    3. 重命名子ct的键(按照key_X的规格) d、 将l1和l2作为条目添加到字典中,基本上将子列表和子列表合并到一个字典中

一旦有了字典列表,pandas就能够识别它,并且可以将它插入到pd.DataFrame实例化器对象中

相关问题 更多 >