I have a dataframe that contains multiple rows of dict values. I want to combine all the rows into one row. Since my dataframe contains dict values, I get an error. Is it possible to concatenate dict values to one line?
I have a dataframe like below,
Thanks Advance
CodePudding user response:
If you're wanting to merge dictionaries then use this example
df = pd.DataFrame()
df["dicts"] = [{"this":"is"}, {"a":"collection"}, {"of":"dicts", "the":"end"}]
solution
import itertools
dict(itertools.chain.from_iterable(d.items() for d in df["dicts"]))
This gives you
{'this': 'is', 'a': 'collection', 'of': 'dicts', 'the': 'end'}