5

When i try to convert tuples to pandas dataframe i get the following error:

DataFrame constructor not properly called!

I am using the following code

columnlist=["Timestamp","Price","Month","Day","DayofWeek","tDaysleftMonth","tDayinMonth","tDayinWeek"]
tickerData=pd.DataFrame(tickerDataRaw,columns=columnlist)

The data was loaded to tuples from a MySQL database ,

Please find a screenshot of the data. Data I am trying to convert

1
  • I am using the following code columnlist=["Timestamp","Price","Month","Day","DayofWeek","tDaysleftMonth","tDayinMonth","tDayinWeek"] tickerData=pd.DataFrame(tickerDataRaw,columns=columnlist) Commented Jun 16, 2016 at 14:31

2 Answers 2

7

I think you can use DataFrame.from_records with converting tuples to list:

import pandas as pd

tuples = ((1,2,3),(4,6,7),(7,3,6),(8,2,7),(4,6,3),(7,3,6))

columnlist = ['a','b','c']
df = pd.DataFrame.from_records(list(tuples), columns=columnlist)
print (df)
   a  b  c
0  1  2  3
1  4  6  7
2  7  3  6
3  8  2  7
4  4  6  3
5  7  3  6

Another solution with DataFrame constructor only:

import pandas as pd

tuples = ((1,2,3),(4,6,7),(7,3,6),(8,2,7),(4,6,3),(7,3,6))

columnlist = ['a','b','c']
df = pd.DataFrame(list(tuples), columns=columnlist)
print (df)
   a  b  c
0  1  2  3
1  4  6  7
2  7  3  6
3  8  2  7
4  4  6  3
5  7  3  6

EDIT:

If check DataFrame and parameter data:

data : numpy ndarray (structured or homogeneous), dict, or DataFrame

Dict can contain Series, arrays, constants, or list-like objects

Sign up to request clarification or add additional context in comments.

Comments

0

According to the Dataframe documentation page, data is required to be

numpy ndarray (structured or homogeneous), dict, or DataFrame

The easiest way to resolve your problem is simplty load your data in a numpy array and it should work fine.

>>> tuples = ((1,2,3),(1,2,3),(1,2,3))
>>> columns = ["A", "B", "C"]
>>> pd.DataFrame(tuples, columns=columns)
PandasError: DataFrame constructor not properly called!

>>> pd.DataFrame(np.array(tuples), columns=columns)
   A  B  C
0  1  2  3
1  1  2  3
2  1  2  3

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.