为了账号安全,请及时绑定邮箱和手机立即绑定

将一列对象值转换为浮点数或整数。ValueError:int() 的无效文字

将一列对象值转换为浮点数或整数。ValueError:int() 的无效文字

陪伴而非守候 2021-06-17 14:24:45
dfproduction = pd.read_csv('https://raw.githubusercontent.com/chessybo/Oil-Spill-map/master/Oil%20Spill%20Data%20-%20Crude%20Oil%2C%20Gas%20Well%20Liquids%20or%20Associated%20Products%20(H-8)/production%20data/Crude%20Oil%20Production%20and%20Well%20Counts%20(since%201935).csv', encoding='utf-8')我想将此数据转换为数字(即列,“原油生产(Mbbl)”),例如 float 或 int。目前 dtype 是对象    print(dfproduction.dtypes)MasterYear                                  int64Crude Oil Production (Mbbl)                objectDaily Avg. Production (Mbbl/day)           objectNumber of Producing Wells                  objectPercent Change in Production               objectAvg. Per Well Production (bbl/day)        float64Crude Oil Reserves as of Jan. 1 (Mbbl)     objectinfo                                       objectdtype: object然而,任何这样做的尝试都会导致某种形式的错误。dfproduction['Crude Oil Production (Mbbl)'].astype('int')ValueError: invalid literal for int() with base 10: '1,026,765'dfproduction['Crude Oil Production (Mbbl)'].astype('float')ValueError: could not convert string to float: '375,617'更新:问题是数字中的逗号,我删除了逗号并重新上传了数据。直到现在我收到以下错误..UnicodeDecodeError: 'utf-8' codec can't decode byte 0xa0 in position 83: invalid start byte
查看完整描述

1 回答

?
斯蒂芬大帝

TA贡献1827条经验 获得超8个赞

使用str.replace()删除逗号。

dfproduction['Crude Oil Production (Mbbl)'].str.replace(r',', '').astype('int')


查看完整回答
反对 回复 2021-06-29
  • 1 回答
  • 0 关注
  • 129 浏览
慕课专栏
更多

添加回答

举报

0/150
提交
取消
微信客服

购课补贴
联系客服咨询优惠详情

帮助反馈 APP下载

慕课网APP
您的移动学习伙伴

公众号

扫描二维码
关注慕课网微信公众号