[英]Pandas UserWarning when using df.loc based on condition
I've below dataframe where I want to change values based on condition我低于 dataframe 我想根据条件更改值
name checker data
0 user1 user122AB 10
1 user2 user132AB 12
2 user3 user346AB 14
3 user4 user108AB 16
4 user5 user122CD 10
5 user6 user132CD 12
6 user7 user346CD 14
7 user8 user108CD 16
Expected output:预期 output:
name checker data
0 user1 user122AB 10
1 user2 user132AB 12
2 user3 user346AB 14
3 user4 user108AB 16
4 user5 user122CD 900
5 user6 user132CD 900
6 user7 user346CD 900
7 user8 user108CD 900
I'm using below code for same.我正在使用下面的代码。
import pandas as pd
import re
df = pd.DataFrame([["user1", "user122AB", "10"], ["user2", "user132AB", "12"], ["user3", "user346AB", "14"], ["user4", "user108AB", "16"], ["user5", "user122CD", "10"], ["user6", "user132CD", "12"], ["user7", "user346CD", "14"], ["user8", "user108CD", "16"]], columns=["name", "checker", "data"])
df.loc[df.checker.str.contains("\d+\w*(CD)$"), "data"] = "900"
print(df)
It is doing the exact thing that I want but throwing below warning along with output.它正在做我想要的确切事情,但与 output 一起抛出低于警告。
UserWarning: This pattern has match groups. To actually get the groups, use str.extract.
Please help if I'm doing something wrong or please share any better way to achieve this.如果我做错了什么,请提供帮助,或者请分享任何更好的方法来实现这一点。
You have this warning because of the parenthesis.由于括号,您会收到此警告。 You are searching for matching patterns, to be returned.您正在搜索要返回的匹配模式。 I think this is unnecessary.我认为这是不必要的。 pandas
(1.0.3) is coded to send a warning is the regex match one ore more parenthesis patterns ( if regex.groups > 0: warnings.warn(...)
). pandas
(1.0.3) 被编码为发送警告是正则表达式匹配一个或多个括号模式( if regex.groups > 0: warnings.warn(...)
)。
The line below is then working without warning:然后下面的行在没有警告的情况下工作:
df.loc[df.checker.str.contains("\d+\w*CD$"), "data"] = "900"
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.