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

Viewed 2614

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?

3 Answers

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

df = pd.DataFrame(tuples, columns=tuples[0])[1:]
Related