Home > Back-end >  Python: list of lists and string to dataframe
Python: list of lists and string to dataframe

Time:10-06

I have a list with the following format:

data = [
    ['string1', [a, b, c]],
    ['string2', [d, e, f]],
    ['string3', [g, h, i]],
    ['string4', [j, k, l]],
]

and I would like to transform it into a data frame like so, with the strings as column names and the lists as column content:

string1     string2     string3     string4
a           d           g           j
b           e           h           k
c           f           i           l

How can I achieve that?

CodePudding user response:

Convert values to dictionary and then to DataFrame constructor:

df = pd.DataFrame(dict(data))
print (df)
  string1 string2 string3 string4
0       a       d       g       j
1       b       e       h       k
2       c       f       i       l
  • Related