[英]Why is `pandas.read_csv` not the reciprocal of `pandas.DataFrame.to_csv`?
[英]pandas.read_csv returns empty dataframe
這是我的 csv 文件:
uipid,shid,pass,camera,pointheight,pointxpos,PointZPos,deffound,HighestHeight,XPosition,ZPosition,RLevel,Rejected,MixedP
50096853911,6345214,1,SXuXeXCamera,218,12600,82570,no,-1,-1,-1,880,no,498
49876879038,6391743,1,SZuZeZCamera,313,210400,187807,no,-1,-1,-1,880,no,388
這是我的代碼:
df=pd.read_csv('.\sources\data.csv', delimiter=',', names=['uipid','shid','pass','camera','pointheight','pointxpos','PointZPos','deffound','HighestHeight', 'XPosition','ZPosition','RLevel','Rejected','MixedP'], skip_blank_lines=True, skipinitialspace=True, engine='python')
當我選擇一列print(df.loc[(df['uipid']==50096853911))
我得到一個空的 df。
空數據幀列[uipid,shid,pass,camera,pointheight,pointxpos,PointZPos,deffound,HighestHeight,XPosition,ZPosition,RLevel,Rejected,MixedP] 索引:[]
而當我設的dtype
在pd.read_csv
:
df=pd.read_csv('.\sources\data.csv', delimiter=',' ,dtype={'uipid':int, 'shid': int, 'pass':int, 'camera':str, 'pointheight':int, 'pointxpos':int , 'PointZPos':int, 'deffound':str, 'HighestHeight':int, 'XPosition':int,'ZPosition':int, 'RLevel':int, 'Rejected':str, 'MixedP':int}, names=['uipid','shid','pass','camera','pointheight','pointxpos','PointZPos','deffound','HighestHeight', 'XPosition','ZPosition','RLevel','Rejected','MixedP'], skip_blank_lines=True, index_col=False, encoding="utf-8", skipinitialspace=True)
我收到此錯誤:
類型錯誤:無法根據規則“安全”將數組從 dtype('O') 轉換為 dtype('int32')
ValueError:int() 的無效文字,基數為 10:'uipid'
嘗試將header = 0
放在您的第二個read_csv
示例中,並讓我們知道它是否有效。
嘗試這個:
df_trail=pd.read_csv('/content/New Text Document.txt',
delimiter=',',
names=['uipid', 'shid', 'pass', 'camera', 'pointheight', 'pointxpos', 'PointZPos', 'deffound', 'HighestHeight', 'XPosition', 'ZPosition', 'RLevel', 'Rejected', 'MixedP'],
skip_blank_lines=True, skipinitialspace=True, engine='python',header=0)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.