4

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
1
  • 1
    why not simply pd.DataFrame(tups, columns=['Values']+columns) ? Commented Mar 14, 2016 at 11:34

2 Answers 2

8

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
Sign up to request clarification or add additional context in comments.

Comments

5

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.