【Pandas】笔记内容001:“FutureWarning: The default value of regex will change from True to False...“解释说明

背景

使用pandas中的.str.replace()进行文本内容转变时,得到一个提醒FutureWarning: The default value of regex will change from True to False in a future version.。通过查阅,现将解决方式记录,以供未来再遇到时进行参考。

A u t h o r : S o p h e r Author: Sopher Author:Sopher
I d e n t i f i c a t i o n : S t u d e n t Identification: Student Identification:Student
D e s c r i p t i o n : Description: Description: 十年前种树是最好的时间,但现在也不晚!

过程说明

1.今天使用.str.replace()将表格数据中单价文本形式进行转化为Float过程中,遇到警告说明:FutureWarning: The default value of regex will change from True to False in a future version. pd_listings['price'] = pd_listings.price.str.replace(r"\$|,", '').astype(float)

2.原代码是这样的:

# 其中pd_listings是数据表装换为DataFrame
# price表示为价格特征
pd_listings['price'] = pd_listings.price.str.replace(r"\$|,", '').astype(float)

价格原数据格式:
在这里插入图片描述
价格转换后数据格式:
在这里插入图片描述

修改解释

  • 在Pandas未来的版本中,.str.replace()regex的默认值将从True变为False
  • 当regex=True时,单字符正则表达式不会被视为文本字符串
  • 因为我们是针对price中两个单个字符进行操作,因此设置regex=True

代码修改如下:

pd_listings['price'] = pd_listings.price.str.replace(r"\$|,", '',regex=True).astype(float)

此时,代码不会再出现警告

Q W Q , 继 续 坚 持 ! ! ! QWQ, 继续坚持!!! QWQ,

  • 13
    点赞
  • 18
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 4
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 4
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

Chuckie今天也要学习!

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值