python-3.x - 使用否定与熊猫替换
问题描述
我有兴趣使用 pandas dataframe.replace() 但有一个否定。我在文档中没有看到任何支持这一点的内容,那么有没有更好的方法来解决这个问题?
示例数据框
Color Shape
blue star
red triangle
purple square
替换所有不以梯形开头的实例的代码
dataframe['Shape'] = dataframe['Shape'].replace(~['star'], 'trapezoid')
预期数据框
Color Shape
blue star
red trapezoid
purple trapezoid
解决方案
如果使用 .replace 不是必需的,那么这可行
df
Color Shape
0 blue star
1 red triangle
2 purple square
df.Shape[df.Shape != "star"] = "trapezoid"
df
Color Shape
0 blue star
1 red trapezoid
2 purple trapezoid
推荐阅读
- go - for 循环中的短格式 var 声明
- oop - 继承实现的设计问题
- adobe-embed-api - 是否可以在 adobe embed api 中创建自定义注释
- ios - 在 iOS 上,节点服务器的会话 cookie 被初始化
- javascript - 当可见数据点全为零时,您可以隐藏 amCharts 图表中的图形吗?
- c# - 我有一个使用 c# 创建的 Azure Web 函数。并出现函数应用超时和主机关闭 4 分钟的间歇性问题
- python - 匹配数据名中具有相同位数的两列中的值
- python - 在匹配中将数组的值加倍 - Python
- arm - 在 macOS Intel 上生成和运行 ARMv8 SVE 二进制文件
- r - 结合5个ggplots