[英]pandas convert column to ints not working
我正在嘗試從dfA
中刪除也在dfB
。
事情是dfA
是對象類型,因此我嘗試將其轉換為int。 這是代碼:
dfA = pd.read_excel('small_file.xlsx',header=None)
dfB = pd.read_csv('large_file.csv',header=None)
dfA = dfA.convert_objects(convert_numeric=True)
dfA[0] = pd.to_numeric(dfA[0],errors='coerce')
dfA = dfA.dropna()
# converting to int
dfA[0] = dfA[0].astype(int) # THIS line gets error
df_output = dfA[~dfA[0].isin(dfB[0])]
這就是dfA的樣子
0
0 2293365227
1 3045897298
2 8162414592
3 9312969810
... ...
和dfB
0
0 2030000000
1 2030156119
2 2030389149
... ...
我收到此錯誤:
ValueError:以10為基數的long()的無效文字:'Goulding'
似乎有一些值-可能是字符串'Goulding'
,無法將其轉換為int。
您可以使用to_numeric並在有問題的值處獲取NaN:
dfA[0] = pd.to_numeric(dfA[0], errors='coerce')
在將整個列轉換為整數之前。
如果您還可以共享excel文件,我可以仔細看看。
對於此類錯誤,最好仔細觀察一下。
采用:
dfA.loc[dfA[0].str.contains('Goulding')]
查找存在索引的索引並查看發生了什么。 然后創建一個過濾掉不良數據並將其應用於序列的函數。 如果遇到其他錯誤,請沖洗並重復。
例:
def replace_str(x):
return re.search('\d+',x).group(0)
dfA[0] = dfA[0].apply(replace_str)
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.