Reputation: 97
I have a pandas dataframe which has dict as values. I would like to transform this dataframe into the format in expected result.
and i want to split the columns into each keys of the dict. For example for the first columns 'questionnaire', i want 'questionnaire.step', 'questionnaire.lastCompletedStep' and so on.
the issue here it's that json normalization doesn't works:
json_normalize(data=data)
someone know how to fix it?
Upvotes: 0
Views: 151
Reputation: 66
Is the column value in the format of type string
? If it is then you can try this.
I tried this on a dataframe and it worked. Iterate over each column values convert them from str
to dict
then iterate over those values create a new column with key value and assign the value to the column.
data = [str({"step": 7, "lastCompletedStep": 7})]
df = pd.DataFrame(data, columns=["questionaire"])
df
dataval = df.questionaire[0]
print(type(dataval))
result = eval(dataval)
print(result)
print(type(result))
for i,(k, v) in enumerate(result.items()):
df["questionaire"+"."+str(k)] = v
df
Upvotes: 1