Notice: This page requires JavaScript to function properly.
Please enable JavaScript in your browser settings or update your browser.
Lære Challenge | Preprocessing Data: Part II
Data Manipulation using pandas

book
Challenge

Let's replace the negative values in the 'empinch' and 'invsth' columns with zeros using the .where() method.

Opgave

Swipe to start coding

  1. Select the 'empinch' and 'invsth' columns.
  2. Apply the .where() method to chosen columns.
  3. Set the condition what values must be replaced (these must be negative values). Remember, values that don't satisfy this condition will be replaced.
  4. Set the parameter what values must be used instead of replaced ones.

Løsning

# 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())

Var alt klart?

Hvordan kan vi forbedre det?

Tak for dine kommentarer!

Sektion 2. Kapitel 5
# 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())

Spørg AI

expand
ChatGPT

Spørg om hvad som helst eller prøv et af de foreslåede spørgsmål for at starte vores chat

some-alt