To create a new binary column in pandas based on a condition, you can use the np.where()
function from the NumPy library. Here's an example:
main.py207 chars11 lines
This will output the following dataframe:
main.py48 chars7 lines
In this example, we create a new column 'B' based on the condition df['A'] > 3
. If the condition is True, we assign a value of 1 to the corresponding row, otherwise 0.
gistlibby LogSnag