[英]Pandas will not input data into dataframe in the correct order
我最初試圖運行以下代碼:
import pandas as pd
import numpy as np
f = ['Austria', '11m/18d/19yyy', 'Yes', 'No', 'Yes', 'Yes', 'Yes', 1, 'JVI Residences', 2, 1, 'Brief additional comments.']
test= pd.DataFrame(f, columns ={'Country' , 'Date of last observation' , 'ResRep/RTAC/RTC (Yes/No?)' ,
'Available Facilities (Yes/No?)' , 'Funds Transferrable (Yes/No?)' ,
'Local Flight Help (Yes/No?)' , 'Visa Help (Yes/No?)' ,
'Local Support Rating 1 Great - 3 Bad' , 'Hotel Name' ,
'Hotel Rating 1 Great - 3 Bad' ,
'Travel Route Rating 1 Great - 3 Bad' , 'Additional Overall Comments'})
一直導致以下錯誤:
ValueError: Shape of passed values is (12, 1), indices imply (12, 12)
因此,我試圖通過在創建 dataframe 時將 F 括起來來糾正這一點。但是,這導致 pandas 返回一個數據幀,其中列名亂序並且數據屬於不應屬於的類別:
Available Facilities (Yes/No?) ... Country
0 Austria ... Brief additional comments.
[1 rows x 12 columns]
Index(['Available Facilities (Yes/No?)', 'Hotel Name',
'Local Support Rating 1 Great - 3 Bad', 'Additional Overall Comments',
'Date of last observation', 'Local Flight Help (Yes/No?)',
'ResRep/RTAC/RTC (Yes/No?)', 'Travel Route Rating 1 Great - 3 Bad',
'Visa Help (Yes/No?)', 'Hotel Rating 1 Great - 3 Bad',
'Funds Transferrable (Yes/No?)', 'Country'],
dtype='object')
有人可以建議如何將我的數據 go 轉換為 dataframe 我正在嘗試制作並按照我想要的順序保存嗎?
您已將一個set
作為參數columns
的值。 Pandas 將獲取集合並嘗試將其轉換為列表。 但是,由於set
是無序數據結構,因此在將其從 set 轉換為 list 時無法保留您的列名的順序。 簡單地說,使用list
而不是set
。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.