程序问答   发布时间:2022-06-01  发布网站:大佬教程  code.js-code.com
大佬教程收集整理的这篇文章主要介绍了使用字典替换值大佬教程大佬觉得挺不错的,现在分享给大家,也给大家做个参考。

如何解决使用字典替换值?

开发过程中遇到使用字典替换值的问题如何解决?下面主要结合日常开发的经验,给出你关于使用字典替换值的解决方法建议,希望对你解决使用字典替换值有所启发或帮助;

正则表达式替换不起作用的原因是什么?我试过确保没有多余的空间。

df.column
0    Test_With_Him
1    And_another option with him
2    and_another reason with her

replacement = {'_':' ',Test With': 'test with','and another': 'And another& AND'}
df['column'] = df.column.replace('\s+',' ',regex=True).str.strip().replace(replacement,regex=True)

当我做 df.loc[df['column']=="and another reason with her"] 时什么都没有改变。

解决方法

请使用df.replace(regex=dict)

df=pd.DataFrame({'test':["Test With Him","And Another option with him",'and another reason with her']})

replacement = {r'Test With': 'test with',r'And Another': 'And another& AND'}

df=df.replace(regex=replacement)

                        test
0                     test with Him
1  And another& AND option with him
2       and another reason with her

大佬总结

以上是大佬教程为你收集整理的使用字典替换值全部内容,希望文章能够帮你解决使用字典替换值所遇到的程序开发问题。

如果觉得大佬教程网站内容还不错,欢迎将大佬教程推荐给程序员好友。

本图文内容来源于网友网络收集整理提供,作为学习参考使用,版权属于原作者。
如您有任何意见或建议可联系处理。小编QQ:384754419,请注明来意。
标签: