df = df['Consequence Number'].fillna("CLS" + df.index.astype(str))

我确实try 了上面的解决方案以及迭代行

 for index in priority_cls.iterrows():
     if priority_cls['Consequence Number'].isna() == True:
        priority_cls['Consequence Number'] = 'CLS00'+ str(priority_cls['ConsequenceNumber'].isnull().sum())

错误如下:

TypeError:"Value"参数必须是标量、Dict或Series,但您传递了"Index"

推荐答案

转换您的指数to_series:

df['Consequence Number'].fillna("CLS" + df.index.to_series().astype(str))

示例:

df = pd.DataFrame({'Consequence Number': ['A', 'B', pd.NA, pd.NA]})

df['out'] = (df['Consequence Number']
             .fillna("CLS" + df.index.to_series().astype(str))
            )

输出:

  Consequence Number   out
0                  A     A
1                  B     B
2               <NA>  CLS2
3               <NA>  CLS3

Python-3.x相关问答推荐

是什么原因导致Pandas=2.1.4和Pandas=1.4.2之间Pandas DataFrame中从Float64到int32的连续列转换方式不同

如何将多个字典合并到一个列中,并为不同的行使用相同的键

visual studio代码窗口中未激活虚拟环境11

替换 .txt 文件中的项目列表

pip install saxonche v 12.1.0 产生 FileNotFoundError

将水平堆叠的数据排列成垂直

根据按不同列中的值分组的平均值划分 DataFrame

Pytest顺序测试A,然后测试B,然后再测试A

如何键入提示函数,在 Python 中通过类decorator 添加到类中

删除Pandas 数据框行不起作用

使用大型多个数据集,其中每个数据集包含多个值 - Pytorch

对齐文本文件中的列

Pytorch:图像标签

Python socket.error: [Errno 13] 权限被拒绝

使用 python 正则表达式匹配日期

使用 distutils 分发预编译的 python 扩展模块

如何为 Python 中的线程设置异步事件循环?

python 3的蓝牙库

如何使用 Python 订阅 Websocket API 通道?

在 Ipython 中使用 Pylint (Jupyter-Notebook)