Challenge
Let's replace the negative values in the 'empinch'
and 'invsth'
columns with zeros using the .where()
method.
Task
Swipe to start coding
- Select the
'empinch'
and'invsth'
columns. - Apply the
.where()
method to chosen columns. - Set the condition what values must be replaced (these must be negative values). Remember, values that don't satisfy this condition will be replaced.
- Set the parameter what values must be used instead of replaced ones.
Solution
9
1
2
3
4
5
6
7
8
9
# Importing the library
import pandas as pd
# Reading the file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data3.csv')
# Perform a replacement
df[['empinch', 'invsth']] = df[['empinch', 'invsth']].where(~(df[['empinch', 'invsth']] < 0), other = 0)
# Analyze column values after replacement
print(df[['empinch', 'invsth']].describe())
Everything was clear?
Thanks for your feedback!
Section 2. Chapter 5
9
1
2
3
4
5
6
7
8
9
# Importing the library
import pandas as pd
# Reading the file
df = pd.read_csv('https://codefinity-content-media.s3.eu-west-1.amazonaws.com/f2947b09-5f0d-4ad9-992f-ec0b87cd4b3f/data3.csv')
# Perform a replacement
df[['empinch', 'invsth']] = df[[___]].___((df[['empinch', 'invsth']] ___), other = ___)
# Analyze column values after replacement
print(df[['empinch', 'invsth']].describe())
Ask AI
Ask anything or try one of the suggested questions to begin our chat