简体   繁体   English

pandas.read_csv 返回空数据框

[英]pandas.read_csv returns empty dataframe

here is my csv file:这是我的 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

Here is my code:这是我的代码:

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

and when I select a column print(df.loc[(df['uipid']==50096853911)) I get an empty df.当我选择一列print(df.loc[(df['uipid']==50096853911))我得到一个空的 df。

Empty DataFrame Columns[uipid,shid,pass,camera,pointheight,pointxpos,PointZPos,deffound,HighestHeight,XPosition,ZPosition,RLevel,Rejected,MixedP] Index: []空数据帧列[uipid,shid,pass,camera,pointheight,pointxpos,PointZPos,deffound,HighestHeight,XPosition,ZPosition,RLevel,Rejected,MixedP] 索引:[]

And when i set the dtype in pd.read_csv :而当我设的dtypepd.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)

I get this error:我收到此错误:

TypeError: Cannot cast array from dtype('O') to dtype('int32') according to the rule 'safe'类型错误:无法根据规则“安全”将数组从 dtype('O') 转换为 dtype('int32')

ValueError: invalid literal for int() with base 10: 'uipid' ValueError:int() 的无效文字,基数为 10:'uipid'

尝试将header = 0放在您的第二个read_csv示例中,并让我们知道它是否有效。

Try this:尝试这个:

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.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM