简体   繁体   中英

Convert list of tuples to dataframe - where first element of tuple is column name

I have a list of tuples in the format:

tuples = [('a',1,10,15),('b',11,0,3),('c',7,19,2)]  # etc.

I wish to store the data in a DataFrame with the format:

      a       b     c      ...  

0     1       11     7     ...   
1     10      0      19    ...  
2     15      3      2     ...   

Where the first element of the tuple is what I wish to be the column name.

I understand that if I can achieve what I want by running:

df = pd.DataFrame(tuples)
df = df.T
df.columns = df.iloc[0]
df = df[1:]

But it seems to me like it should be more straightforward than this. Is this a more pythonic way of solving this?

Here's one way

In [151]: pd.DataFrame({x[0]:x[1:] for x in tuples})
Out[151]:
    a   b   c
0   1  11   7
1  10   0  19
2  15   3   2

You can use dictionary comprehension, like:

pd.DataFrame({k:v for k,*v in tuples})

in , or:

pd.DataFrame({t[0]: t[1:] for t in tuples})

in .

which generates:

>>> pd.DataFrame({k:v for k,*v in tuples})
    a   b   c
0   1  11   7
1  10   0  19
2  15   3   2

The columns will be sorted alphabetically .

If you want the columns to be sorted like the original content , you can use the columns parameter:

pd.DataFrame({k:v for k,*v in tuples})

again in , or for :

pd.DataFrame({t[0]: t[1:] for t in tuples})

We can shorten this a bit into:

from operator import itemgetter

pd.DataFrame({t[0]: t[1:] for t in tuples},columns=tuples)

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