python - 获取 key 错误 : 61 while iterating through the rows

标签 python pandas

在我的数据集中,我的一列是 Name,我创建的另一列是 IncStr。如果列中的名称包含 Rev,我想将 IncStr 转换为由全零组成的 1。为此,我编写了以下代码,但得到了 KeyError: 61:

for i in range(0,len(df)):
    if ((df['Name'].str.contains('Rev'))[i] == True):
               df['IncStr'] == 1

最佳答案

我认为在 pandas 中最好尽可能避免循环 - 这里将 bool 掩码转换为整数 - True, False1,0:

df['IncStr'] = df['Name'].str.contains('Rev').astype(int)

关于python - 获取 key 错误 : 61 while iterating through the rows,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/53363553/

相关文章:

python - 使用 Python 创建多页 TIFF

python - 顶级棉花糖模式验证

python - 如何按索引对列表列进行切片

python - Pandas - 正确使用 Apply 函数

python - XML 到 CSV 转换器

python - 值错误 : Couldn't broadcast input array from shape (51) to (51, 1)

python - 使用 numpy.argpartition 忽略 NaN

python - 执行字典中的函数 | python 3

python - pandas groupby 两个相似的列和两个不同的列

python - 根据列将一个数据框映射到另一个数据框