首页 > 解决方案 > Flip 0 and 1 bits in pandas DataFrame efficiently?

问题描述

I had an issue switching a binary variable in-place for a large DF... this ended up working; posting in case this is an issue for another user.

dfSet2['TargetDefault'] = dfSet2.apply(
    lambda x: (0 if x['TargetDefault']==1 else 1), axis=1)

Is there a faster/easier way to do this?

enter image description here

标签: pythonpandasdataframe

解决方案


这很简单

dfSet2['TargetDefault'] = 1 - dfSet2['TargetDefault']

推荐阅读