简体   繁体   English

从 DataFrame 创建字典?

[英]Create a dictionary from DataFrame?

I want to create a dictionary from a dataframe in python.我想从 python 中的 dataframe 创建一个字典。 In this dataframe, frame one column contains all the keys and another column contains multiple values of that key.在此 dataframe 中,框架的一列包含所有键,另一列包含该键的多个值。

DATAKEY      DATAKEYVALUE
name         mayank,deepak,naveen,rajni
empid        1,2,3,4
city         delhi,mumbai,pune,noida

I tried this code to first convert it into simple data frame but all the values are not separating row-wise:我尝试使用此代码首先将其转换为简单的数据框,但所有值都没有按行分隔:

columnnames=finaldata['DATAKEY']
collist=list(columnnames)
dfObj = pd.DataFrame(columns=collist)
collen=len(finaldata['DATAKEY'])
for i in range(collen):
    colname=collist[i]
    keyvalue=finaldata.DATAKEYVALUE[i]
    valuelist2=keyvalue.split(",")
    dfObj = dfObj.append({colname: valuelist2}, ignore_index=True)

You should modify you title question, it is misleading because pandas dataframes are "kind of" dictionaries in themselves, that is why the first comment you got was relating to the .to_dict() pandas' built-in method.您应该修改标题问题,因为 pandas 数据帧本身就是“一种”字典,这就是为什么您得到的第一条评论与.to_dict()熊猫的内置方法有关。

What you want to do is actually iterate over your pandas dataframe row-wise and for each row generate a dictionary key from the first column, and a dictionary list from the second column.您想要做的实际上是逐行迭代您的 pandas dataframe 并为每一行从第一列生成一个字典键,从第二列生成一个字典列表。

For that you will have to use:为此,您将不得不使用:

  • an empty dictionary: dict()空字典: dict()
  • the method for iterating over dataframe rows: dataframe.iterrows()遍历 dataframe 行的方法: dataframe.iterrows()
  • a method to split a single string of values separated by a separator as the split() method you suggested: str.split() .一种将由分隔符分隔的单个值字符串拆分为您建议的split()方法的方法: str.split()

With all these tools all you have to do is:使用所有这些工具,您所要做的就是:

output = dict()
for index, row in finaldata.iterrows():
    output[row['DATAKEY']] = row['DATAKEYVALUE'].split(',')

Note that this generates a dictionary whose values are lists of strings.请注意,这会生成一个字典,其值是字符串列表。 And it will not work if the contents of the 'DATAKEYVALUE' column are not singles strings.如果 'DATAKEYVALUE' 列的内容不是单字符串,它将不起作用。

Also note that this may not be the most efficient solution if you have a very large dataframe.另请注意,如果您有一个非常大的 dataframe,这可能不是最有效的解决方案。

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

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