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
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.
We can create pandas dataframe by using 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.
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.
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
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With