Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Error while converting tuples to Pandas DataFrame

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

like image 821
Noufal E Avatar asked Jun 16 '16 14:06

Noufal E


People also ask

Can you store a tuple in a DataFrame?

We can create a DataFrame from a list of simple tuples, and can even choose the specific elements of the tuples we want to use.

Can we create pandas DataFrame using dictionary of tuples?

We can create pandas dataframe by using tuples.

How do you convert a DataFrame to a list of tuples?

Method 1: Using collect() method By converting each row into a tuple and by appending the rows to a list, we can get the data in the list of tuple format.

What does .values in pandas do?

The values property is used to get a Numpy representation of the DataFrame. Only the values in the DataFrame will be returned, the axes labels will be removed. The values of the DataFrame. A DataFrame where all columns are the same type (e.g., int64) results in an array of the same type.


1 Answers

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

like image 92
jezrael Avatar answered Oct 05 '22 00:10

jezrael