Home > database >  How to populate a column in a df based on conditions in Python
How to populate a column in a df based on conditions in Python

Time:02-01

I am new to programming and self taught so excuse my limited knowledge.

I have a df that looks like this:

import pandas as pd

df1= pd.DataFrame.from_dict({
    'Description': ['This car is german', 'This vehicle is french', 'This automobile is british', 'This car is british', 'This thing is british'], 
    'SUV?': ['Yes', 'No', 'No', 'Yes', 'Yes'],
    'Action': [' ', ' ', ' ', ' ', ' '],
    })
df1

What I would like to do is to populate the column "Action" with the string "buy" if the word 'british' is present in the column "Description" and the word "Yes" is present in the column "SUV?"

I tried to use the lambda function but I can only make it work with one of the conditions. For instance:

df1["Action"] = df1['Description'].apply(lambda x: "Buy" if "british" in x else "0")

If someone could put on the right track I would appreciate it a lot!

CodePudding user response:

Using np.where() method import numpy as np

df1['Action'] = (np.where((df1['Description'].str.contains('british')) 
                    & (df1['SUV?'] == 'Yes'), 'Buy', ''))
print(df1)

With lambda approach:

df1['Action'] = (df1.apply(lambda x: 'Buy' if 'british' in x["Description"] 
                    and x['SUV?'] == 'Yes' else '', axis=1))
print(df1)

                  Description SUV? Action
0          This car is german  Yes       
1      This vehicle is french   No       
2  This automobile is british   No       
3         This car is british  Yes    Buy
4       This thing is british  Yes    Buy

CodePudding user response:

For example you can use df.loc[] (see doc):

# condition
cond = (df1["Description"].str.contains('british')) & (df1["SUV?"] == "Yes")

# select rows by condidtion and insert value "buy"
df1.loc[cond, "Action"] = "buy"

CodePudding user response:

You can use the pandas DataFrame query method to check for multiple conditions. The query method takes a string argument that contains the conditions to be checked. Here is an example of how to use the query method to populate the 'Action' column with the string 'buy' if both conditions are met:

import pandas as pd

df1 = pd.DataFrame.from_dict({
 'Description': ['This car is german', 'This vehicle is french', 'This automobile is british', 'This car is british', 'This thing is british'], 
 'SUV?': ['Yes', 'No', 'No', 'Yes', 'Yes'], 
 'Action': [' ', ' ', ' ', ' ', ' '],
})

df1.query('Description.str.contains("british") and SUV? == "Yes"', inplace=True)
df1['Action'] = 'buy'

print(df1)

This will output the following DataFrame, with the 'Action' column populated with the string 'buy':

Description SUV?    Action
This car is british Yes buy
This automobile is british  Yes buy
This thing is british   Yes buy
  • Related