python - 将 if then 与 python pandas 一起使用
问题描述
我有一个在 python 中使用 pandas 的数据集,并且想对特定列应用 if-then-else 规则。
如果存在缺失值,则将其替换为从同一观察中的另一列中获取的特定值,否则什么也不做。
我的数据集由以下代码生成:
results = df2.merge(df1,on="sku", how="left")
需要填充的列变量,如果为空是“stock_y”。如果为空,则应将列变量“stock_x”的值复制到“stock_y”。在 stock_y 已经被填满的情况下,代码应该跳过观察。
解决方案
results['stock_y'] = results['stock_y'].combine_first(results['stock_x'])
推荐阅读
- java - 如何在我的 toString 方法中修复我的输出?
- winui-3 - 在“AcrylicBrush”WinUI 类型中找不到属性“backgroundsource”
- webpack - 如何在 quasar js 框架中启用 webpack 实验选项?
- shell - linux中的可执行文件使用(perm)?
- r - 如何计算r中的残差
- performance - 为什么在最坏的情况下,进位超前加法器被认为比波纹进位更好?
- python - RuntimeError:事件循环已关闭 - JanusGraph
- php - VS Code 上的 intelephense 错误:预期类型为“字符串”。找到'字符串 []'
- reactjs - React + GSAP:动画元素onClick
- game-development - 如何实例化一个场景并让它只面向 x 或 z 方向?