我想创建一个新列(第三列),根据以下条件在其中添加 1 或 0:
有人可以使用 pandas 编写一个代码来执行此操作吗?请问很紧急
我很困惑如何处理这个案子。请帮忙。
您可以使用 numpys where 函数,它运行速度很快。
df['matched'] = np.where((df['col1'] == df['col2']), 1, 0)
获取
1
和 0
的替代方法:
df['matched'] = df['col1'].eq(df['col2']).astype(int)