Home > Software design >  How can I make dictionary key as column of dataframe?
How can I make dictionary key as column of dataframe?

Time:05-23

!pip install -U LeXmo
from LeXmo import LeXmo
df['Dict'] = df['Content '].apply(lambda x: [LeXmo.LeXmo(x)])

Using this snippet , I am able to generate this

enter image description here

But my desired output is

enter image description here

I want to make each dictionary key of 'Dict' as a seperate column for each row. How can I do this??

CodePudding user response:

You can convert output from LeXmo.LeXmo(x) to Series, so it create new columns if call function in Series.apply, last append to original DataFrame by DataFrame.join:

df = df.join(df['Content '].apply(lambda x: pd.Series(LeXmo.LeXmo(x))))

CodePudding user response:

Do you already have the dict? Then this simple line should do it:

df = pandas.DataFrame.from_dict(myDict)

Here is some more info (in case you haven't used your favorite search engine to solve this problem) link

  • Related