以下是 Python 代码实现:

def check_and_add_c(df, row_index):
    if df.iloc[row_index]['A'] == df.iloc[row_index]['B']:
        df.at[row_index, 'C'] = True
        return 1
    else:
        return 0

其中,df 是待操作的 DataFrame,row_index 是要检查的行的索引。iloc 方法可以通过行索引获取具体的行数据,at 方法可以对 DataFrame 进行原地修改。如果 A 和 B 列相等,则在 C 列增加一个 True 值,并返回 1;否则直接返回 0。

Python DataFrame: 判断两列相等并添加新列

原文地址: https://www.cveoy.top/t/topic/nwFJ 著作权归作者所有。请勿转载和采集!

免费AI点我,无需注册和登录