Kom igångKom igång gratis

Using text filters to remove records

It pays to have to ask your clients lots of questions and take time to understand your variables. You find out that Assumable mortgage is an unusual occurrence in the real estate industry and your client suggests you exclude them. In this exercise we will use isin() which is similar to like() but allows us to pass a list of values to use as a filter rather than a single one.

Den här övningen är en del av kursen

Feature Engineering with PySpark

Visa kurs

Övningsinstruktioner

  • Use select() and show() to inspect the distinct values in the column 'ASSUMABLEMORTGAGE' and create the list yes_values for all the values containing the string 'Yes'.
  • Use ~df['ASSUMABLEMORTGAGE'], isin(), and .isNull() to create a NOT filter to remove records containing corresponding values in the list yes_values and to keep records with null values. Store this filter in the variable text_filter.
  • Use where() to apply the text_filter to df.
  • Print out the number of records remaining in df.

Interaktiv övning med praktiskt arbete

Testa den här övningen genom att slutföra den här exempelkoden.

# Inspect unique values in the column 'ASSUMABLEMORTGAGE'
df.____([____]).distinct().____()

# List of possible values containing 'yes'
yes_values = [____, ____]

# Filter the text values out of df but keep null values
text_filter = ~df['ASSUMABLEMORTGAGE'].isin(____) | df['ASSUMABLEMORTGAGE'].isNull()
df = df.____(text_filter)

# Print count of remaining records
print(____.____())
Redigera och kör kod