Pandas无法更改dataframe列的数据类型

2024-09-26 22:55:03 发布

您现在位置:Python中文网/ 问答频道 /正文

从这里下载一些数据: http://insideairbnb.com/get-the-data.html

然后

listings = pd.read_csv('listings.csv')

尝试更改类型

listings.bathrooms = listings.bathrooms.astype('int64',errors='ignore')
listings.bedrooms = listings.bedrooms.astype('int64',errors='ignore')
listings.beds = listings.beds.astype('int64',errors='ignore')
listings.price = listings.price.replace('[\$,]','',regex=True).astype('float')
listings.price = listings.price.astype('int64',errors='ignore')

尝试了一些其他的组合,但最后出现了错误或只是没有改变数据类型

编辑:更正了一些打字错误


Tags: csv数据comhttp错误pricelistingsignore
1条回答
网友
1楼 · 发布于 2024-09-26 22:55:03

最后一行中的撇号位置不正确,最后一行的类型也不正确:您需要“而不是”。(可能是由于代码块的原因意外添加的)

所以对我来说,它是这样工作的:

listings.price.astype('int64', errors='ignore')

但如果要将其重新分配给原始变量,则需要与前几行中使用的结构相同:

listings.price = listings.price.astype('int64', errors='ignore')

相关问题 更多 >

    热门问题