Change Value of a Dataframe Column Based on a Filter
Question:
I have a Dataframe that consists of 2 columns:
- “Time Spent on website”
- “Dollars spent on the website”
I want to perform some classification analysis on this dataset and I only care whether a user made a purchase or not. So I want to run through the “Dollars spent on the website” column and transform the value to “1” if the user spent over $0.00 and have the value be “0” if the user spent nothing.
What is the proper way to do this with a pandas dataframe?
Answers:
df['purchase'] = 0
df.loc[df['dollars_spent'] > 0, 'purchase'] = 1
or
df['purchase'] = df['dollars_spent'].apply(lambda x: 1 if x > 0 else 0)
You can also use NumPy‘s where
:
import numpy as np
df['Purchase'] = np.where(df['Dollars spent on the website'] > 0, 1, 0)
If the condition is True, 1 is returned else 0.
I have a Dataframe that consists of 2 columns:
- “Time Spent on website”
- “Dollars spent on the website”
I want to perform some classification analysis on this dataset and I only care whether a user made a purchase or not. So I want to run through the “Dollars spent on the website” column and transform the value to “1” if the user spent over $0.00 and have the value be “0” if the user spent nothing.
What is the proper way to do this with a pandas dataframe?
df['purchase'] = 0
df.loc[df['dollars_spent'] > 0, 'purchase'] = 1
or
df['purchase'] = df['dollars_spent'].apply(lambda x: 1 if x > 0 else 0)
You can also use NumPy‘s where
:
import numpy as np
df['Purchase'] = np.where(df['Dollars spent on the website'] > 0, 1, 0)
If the condition is True, 1 is returned else 0.