create a label based on a condition pandas dataframe in python

To create a label based on a condition in pandas dataframe, you can use the loc method provided by pandas. The loc method allows you to access a group of rows and columns by label(s) or a boolean array. Here's how to create a new column based on a condition :

main.py
import pandas as pd

# Create a sample dataframe
df = pd.DataFrame({'score': [75, 92, 68, 85, 77, 89, 91, 82],
                   'name': ['John', 'Emily', 'Harry', 'Samantha', 'Jake', 'Ben', 'Julia', 'Ryan']})

# create a column based on a condition
df.loc[df['score'] >= 80, 'label'] = 'pass'
df.loc[df['score'] < 80, 'label'] = 'fail'
338 chars
10 lines

In this example, we have created a new column 'label' based on a condition if score is greater or equal to 80 it will have "pass" and if it less than 80 it will have "fail".

You can modify the condition based on your requirement. The loc method can also be used for more complex conditions.

main.py
df.loc[(df['score'] >= 80) & (df['name'].str.startswith("J")), 'label'] = 'pass with distinction'
98 chars
2 lines

This will create label 'pass with distinction' for scores greater or equal to 80 and first name starting with 'J'.

gistlibby LogSnag