0

I want to create a dictionary from a dataframe in python. In this dataframe, frame one column contains all the keys and another column contains multiple values of that key.

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)
martineau
  • 119,623
  • 25
  • 170
  • 301

1 Answers1

0

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.

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.

For that you will have to use:

  • an empty dictionary: dict()
  • the method for iterating over dataframe rows: dataframe.iterrows()
  • a method to split a single string of values separated by a separator as the split() method you suggested: 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.

Also note that this may not be the most efficient solution if you have a very large dataframe.

Maxence Bouvier
  • 141
  • 1
  • 6