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.py338 chars10 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.py98 chars2 lines
This will create label 'pass with distinction' for scores greater or equal to 80 and first name starting with 'J'.
gistlibby LogSnag