简体   繁体   English

如何将两个熊猫列转换为字典,但将同一第一列(键)的所有值合并为一个键?

[英]How to convert two pandas columns into a dictionary, but merge all values of same first column (key) into one key?

Sorry for the confusing title, basically I have a Pandas dataframe and I want to convert two columns into a dictionary (with one being the key and the other the value). 抱歉,标题混乱,基本上我有一个Pandas数据框,我想将两列转换成字典(一个是键,另一个是值)。 However, when I use to_dict(), the problem is that since I have many rows in the first column that have the same value, it only takes one of them and I don't get all the data. 但是,当我使用to_dict()时,问题在于,由于第一列中有许多行具有相同的值,因此只需要其中一行,而不能获取所有数据。 Is there a way to work around this? 有办法解决这个问题吗?

I have tried solving this recursively but I haven't been able to figure it out. 我试图递归地解决这个问题,但我一直无法弄清楚。

EDIT: added code 编辑:添加代码

data = pd.read_csv('file')
datalist = []
data2list = []


for i in range(len(data.index)):
    datalist.append(data.loc[i, 'column1'])

for i in range(len(data.index)):
        data2list.append(data.loc[i, 'column2'])

Now datalist has all the values from column1, which I want to be the keys, and column2 has all the values that I want to be the values in the dictionary. 现在,数据列表具有我想要成为键的column1中的所有值,而column2具有想要作为字典中的值的所有值。

The problem however is, the dataframe looks kind of like this: 但是问题是,数据框看起来像这样:

   column1   column2
0  key1      value1
1  key1      value2
2  key2      value3
3  key2      value4

I want the dictionary to look like this: 我希望字典看起来像这样:

dict = {"key1": [value1, value2], "key2": [value3, value4]}

Python dictionaries do not support repeated keys. Python字典不支持重复键。 You could solve this by adjusting the values in your first column so that the keys are not repeated. 您可以通过调整第一列中的值来解决此问题,以免重复键。 Alternatively, you could create a dictionary of lists for each unique key in the first column. 或者,您可以为第一列中的每个唯一键创建一个列表字典。 Since your data is in a Pandas DataFrame, you could do: 由于您的数据在Pandas DataFrame中,因此您可以执行以下操作:

import pandas as pd

# Your data
data = pd.DataFrame({'column1':['key1','key1','key2','key2'],
       'column2':['value1','value2','value3','value3']})

# Grouped dict
data_dict = data.groupby('column1').column2.apply(list).to_dict()       

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

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