简体   繁体   中英

Python: list of lists and string to dataframe

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?

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM