create a new binary column in pandas based on a condition pandas in python

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.py
import pandas as pd
import numpy as np

# Create a sample dataframe
df = pd.DataFrame({'A': [1, 2, 3, 4, 5]})

# Create a new binary column using a condition
df['B'] = np.where(df['A'] > 3, 1, 0)

print(df)
207 chars
11 lines

This will output the following dataframe:

main.py
   A  B
0  1  0
1  2  0
2  3  0
3  4  1
4  5  1
48 chars
7 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.

related categories

gistlibby LogSnag