2016-06-16 187 views
5

當我嘗試轉換的元組到大熊貓數據幀我得到以下錯誤:出錯轉換的元組到大熊貓數據幀

DataFrame constructor not properly called!

我使用下面的代碼

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

的數據加載到來自MySQL數據庫的元組,

請找到數據的屏幕截圖。 Data I am trying to convert

+0

我使用以下代碼 columnlist = [ 「時間戳」, 「價格」, 「月」, 「日」, 「星期幾」, 「tDaysleftMonth」, 「tDayinMonth」, 「tDayinWeek」] tickerData = pd.DataFrame(tickerDataRaw,columns = columnlist) –

+0

如果我的回答很有幫助,請不要忘記[接受](https://stackoverflow.com/help/someone-answers) - 點擊複選標記('v ')旁邊的答案切換從灰色到填充。謝謝。 – jezrael

回答

6

我認爲你可以使用DataFrame.from_records與轉換tupleslist

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 

只有DataFrame構造另一種解決方案:

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 

編輯:

如果檢查DataFrame和參數data

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

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

+0

如果我或'Matt07'的回答很有幫助,請不要忘記[接受](http://meta.stackexchange.com/a/5235/295067)它。謝謝。 – jezrael

0

按照Dataframe documentation page,數據要求是

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

最簡單的方法來解決你的問題是simplty在numpy的陣列加載數據,它應該工作的罰款。

>>> 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