python - Pandas 替换非零值

标签 python pandas

我知道我可以用 df.fillna(0) 替换所有 nan 值并用 df.replace('-',1) 替换单个值,但是如何用单个值替换所有非零值?

最佳答案

使用 bool 索引:

df[df != 0] = value

关于python - Pandas 替换非零值,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/24021909/

相关文章:

python - 无法解决 Python argparse 错误 'object has no attribute'

python - 使用套接字发送 UTF-8

python - 如何在 Pandas Data Frame 中创建条件列,其中列值基于其他列

python - 基于列平均分配 pandas Dataframe

python - 如何使用Series来过滤DataFrame

python - Python 中的参数优化

python - 加入后 Peewee 获取列

python - 功能制作

python - Pandas 事件研究

Python Pandas For 循环