Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

create pandas dataframe from list of tuples

I've seen a couple of similar threads but they didn't really help me hence the new post.

I would like to create the df below from a list of tuples:

Values         Total  extra
label                      
Pictionary  0.000000     12
Chess       4.609929     12
Cluedo      8.421986     12

Here are all the components to make it happen:

columns = ['Total, 'extra']

tups = [(u'Pictionary', 0.0, 12)
        (u'Chess', 4.6099290780141837, 12)
        (u'Cluedo', 8.4219858156028362, 12)]

My failed attempt:

pd.DataFrame(tups, columns=columns)

Error message:

AssertionError: 2 columns passed, passed data had 3 columns
like image 385
Boosted_d16 Avatar asked Dec 25 '22 08:12

Boosted_d16


2 Answers

I think you have to add one value to columns list and then try list comprehension and then set_index with first column, if need first column as index:

import pandas as pd

columns = ['label', 'Total', 'extra']

tups = [(u'Pictionary', 0.0, 12),
        (u'Chess', 4.6099290780141837, 12),
        (u'Cluedo', 8.4219858156028362, 12)]

df = pd.DataFrame([x for x in tups], columns=columns)

print df
        label     Total  extra
0  Pictionary  0.000000     12
1       Chess  4.609929     12
2      Cluedo  8.421986     12

df = df.set_index('label')
#if you need set column name
df.columns.name = 'Values'

print df
Values         Total  extra
label                      
Pictionary  0.000000     12
Chess       4.609929     12
Cluedo      8.421986     12

Or you can use solution by comment of Colonel Beauvel:

import pandas as pd

columns = ['Total', 'extra']

tups = [(u'Pictionary', 0.0, 12),
        (u'Chess', 4.6099290780141837, 12),
        (u'Cluedo', 8.4219858156028362, 12)]

df = pd.DataFrame(tups, columns=['label']+columns)
print df
        label     Total  extra
0  Pictionary  0.000000     12
1       Chess  4.609929     12
2      Cluedo  8.421986     12

df = df.set_index('label')
df.columns.name = 'Values'
print df
Values         Total  extra
label                      
Pictionary  0.000000     12
Chess       4.609929     12
Cluedo      8.421986     12
like image 85
jezrael Avatar answered Dec 30 '22 14:12

jezrael


You can use pandas.DataFrame.from_records()

import pandas as pd

data = [(1,2,3),
        (4,5,6),
        (7,8,9)]

col_names = ['Col0', 'Col1', 'Col2']
row_names = ['Row0', 'Row1', 'Row2']

df = pd.DataFrame.from_records(data, columns=col_names, index=row_names)

print(df)

      Col0  Col1  Col2
Row0     1     2     3
Row1     4     5     6
Row2     7     8     9
like image 27
filiphl Avatar answered Dec 30 '22 16:12

filiphl