python - Pandas 用空白/空字符串替换 NaN

标签 python pandas dataframe nan

我有一个 Pandas 数据框,如下所示:

    1    2       3
 0  a  NaN    read
 1  b    l  unread
 2  c  NaN    read

我想用空字符串删除 NaN 值,使其看起来像这样:

    1    2       3
 0  a   ""    read
 1  b    l  unread
 2  c   ""    read

最佳答案

df = df.fillna('')

这将用 '' 填充 na(例如 NaN)。

inplace 是可能的,但应避免使用 it will be deprecated :

df.fillna('', inplace=True)

仅填充一列:

df.column1 = df.column1.fillna('')

可以使用 df['column1'] 代替 df.column1

关于python - Pandas 用空白/空字符串替换 NaN,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/26837998/

相关文章:

python - 如何为想要付费的人提供 'package' 一个简单的单文件 python 脚本?

python - 如何在fabric中并行执行多个任务

python - S3 批量数据更新

python - 如何使用 pandas 系列对 if 条件进行矢量化?

python - Pandas 数据帧 : Extract Information and Collapse Columns

python - 有没有办法使用 pyvmomi API 删除 guest 虚拟机?

python - 比较两个 pandas 数据帧的内容,即使行的顺序不同

python - 绘制 Pandas Dataframe 的直方图及其均值和标准差,得到 ValueError

r - 将函数应用于数据框的子集但保留整个数据框

python-3.x - 将多分类列转换为 Pandas 中的两个类别