How do I explode a dict column? Explode() did not do the job

Viewed 40

I am loading the following CSV - csv file. I was trying to use explode() to explode the dict formatted column (brochure) into many columns. However, the code leaves the dict intact.

import pandas as pd
df = pd.read_csv('top.csv', index_col=0)
pd.DataFrame(df['brochure'].explode().tolist())

What am I doing wrong?

2 Answers

Try:


from ast import literal_eval

df = pd.read_csv("top.csv", index_col=0)
df["brochure"] = df["brochure"].apply(literal_eval)
df = df["brochure"].explode().apply(pd.Series)
print(df)

Prints:

      lng                      url    updateDate         type            display updateDateFormat creationDateFormat
0    None   Rishum_9_111219418.pdf  1.541506e+12  החמרה לעלון        החמרה לעלון       06.11.2018         31.01.2018
0    None  Rishum_13_120388920.pdf  1.580722e+12  החמרה לעלון        החמרה לעלון       03.02.2020         03.02.2020
0    None   Rishum_18_22329921.pdf  1.610534e+12  החמרה לעלון        החמרה לעלון       13.01.2021         24.12.2020
0  אנגלית   Rishum_18_22325521.pdf  1.610534e+12   עלון לצרכן  עלון לצרכן אנגלית       13.01.2021         24.12.2020
0   עברית   Rishum_18_22328621.pdf  1.610534e+12   עלון לצרכן   עלון לצרכן עברית       13.01.2021         24.12.2020
0   ערבית  Rishum_13_120388520.pdf  1.580722e+12   עלון לצרכן   עלון לצרכן ערבית       03.02.2020         03.02.2020
0    None   Rishum_18_22322021.pdf  1.610534e+12   עלון לרופא         עלון לרופא       13.01.2021         24.12.2020
1    None   Rishum_9_111839418.pdf  1.541671e+12  החמרה לעלון        החמרה לעלון       08.11.2018         30.01.2018
1    None   Rishum_13_79771420.pdf  1.578302e+12  החמרה לעלון        החמרה לעלון       06.01.2020         13.11.2019
1    None   Rishum_18_22389721.pdf  1.610535e+12  החמרה לעלון        החמרה לעלון       13.01.2021         24.12.2020
1    None   Rishum_18_22386121.pdf  1.610535e+12   עלון לרופא         עלון לרופא       13.01.2021         24.12.2020
2    None   Rishum_9_111219418.pdf  1.541506e+12  החמרה לעלון        החמרה לעלון       06.11.2018         31.01.2018
2    None  Rishum_13_120388920.pdf  1.580722e+12  החמרה לעלון        החמרה לעלון       03.02.2020         03.02.2020
2    None   Rishum_18_22329921.pdf  1.610534e+12  החמרה לעלון        החמרה לעלון       13.01.2021         24.12.2020
2  אנגלית   Rishum_18_22325521.pdf  1.610534e+12   עלון לצרכן  עלון לצרכן אנגלית       13.01.2021         24.12.2020
2   עברית   Rishum_18_22328621.pdf  1.610534e+12   עלון לצרכן   עלון לצרכן עברית       13.01.2021         24.12.2020
2   ערבית  Rishum_13_120388520.pdf  1.580722e+12   עלון לצרכן   עלון לצרכן ערבית       03.02.2020         03.02.2020
2    None   Rishum_18_22322021.pdf  1.610534e+12   עלון לרופא         עלון לרופא       13.01.2021         24.12.2020

Just try this instead:

lst = df['brochure'].tolist()
d = {}
for k in lst[0].keys():
  d[k] = [d[k] for d in lst]
df = pd.DataFrame(d)

And now printing df would output the desired DataFrame.

Related