简体   繁体   English

使用 pandas 将 CSV 解析为 python 字典

[英]Parsing CSV to python dictionary using pandas

I have following DataFrame parsed into Python:我将以下 DataFrame 解析为 Python:

df = pd.read_csv("my_file.csv")

result:结果:

   indexes X_values Y_values
0       IDX1     x1      y1
1       IDX1     x2      y2
2       IDX1     x3      y3
3       IDX1     x4      y4
6       IDX2     x1      y1
9       IDX2     x4      y4
10      IDX3     x1      y1
11      IDX3     x2      y2

I need to create dictionaries with each indexes as key and x_values & y_values as list of nested dictionaries.我需要创建字典,每个索引作为键,x_values & y_values 作为嵌套字典的列表。 Output should be like:输出应该是这样的:

{"IDX1" : [{"x1": "y1"}, {"x2": "y2"}, {"x3": "y3"}, {"x4": "y4"}],
"IDX2": [{"x1": "y1"},{"x4": "y4"}],
"IDX3":[{"x1": "y1"}, {"x2": "y2"}]}

I am trying to parse it using set_index() method but always missing something.我正在尝试使用 set_index() 方法解析它,但总是缺少一些东西。 Could you help me?你可以帮帮我吗?

Also, dictionary of nested dictionaries with indexes as keys would be also good solution.此外,以索引为键的嵌套字典的字典也是很好的解决方案。

We can do我们可以做的

d = df[['X_values','Y_values']].apply(lambda x : {x[0]:x[1]},axis=1).groupby(df['indexes']).agg(list).to_dict()
Out[104]: 
{'IDX1': [{'x1': 'y1'}, {'x2': 'y2'}, {'x3': 'y3'}, {'x4': 'y4'}],
 'IDX2': [{'x1': 'y1'}, {'x4': 'y4'}],
 'IDX3': [{'x1': 'y1'}, {'x2': 'y2'}]}

You can try你可以试试

out = (df.apply(lambda row: {row['X_values']: row['Y_values']}, axis=1)
       .groupby(df['indexes']).agg(list).to_dict())
print(out)

{'IDX1': [{'x1': 'y1'}, {'x2': 'y2'}, {'x3': 'y3'}, {'x4': 'y4'}], 'IDX2': [{'x1': 'y1'}, {'x4': 'y4'}], 'IDX3': [{'x1': 'y1'}, {'x2': 'y2'}]}

Sounds like you need to make use of the to_dict() method ( documentation ).听起来您需要使用to_dict()方法( 文档)。

You might need to index like you say.您可能需要像您说的那样进行索引。 I would try:我会尝试:

df.set_index('indexes').T.to_dict('list')

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM