3

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?

Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
tfcoe
  • 391
  • 4
  • 13

3 Answers3

9

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
Zero
  • 74,117
  • 18
  • 147
  • 154
4

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},columns=[k for k,*_ in tuples])

again in , or for :

pd.DataFrame({t[0]: t[1:] for t in tuples},columns=[t[0] 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=map(itemgetter(0),tuples))
Willem Van Onsem
  • 443,496
  • 30
  • 428
  • 555
  • I am currently running python 2.7, so am I correct in thinking starred expressions wont work? Otherwise looks like a good solution! – tfcoe Jul 18 '17 at 15:35
  • @tfcoe: that's correct. Then you better use slice notation. Will update it. – Willem Van Onsem Jul 18 '17 at 15:36
  • Good note on the sorting too. Luckily my data is both previously sorted alphabetically and the desired output is also in alphabetical order. – tfcoe Jul 18 '17 at 15:39
0

Incase if the values in tuple are in row wise, then

df = pd.DataFrame(tuples, columns=tuples[0])[1:]
Vinay Namadi
  • 89
  • 1
  • 4