Applicare trasformazioni avanzate ai DataFrame
pandas offre una miriade di strumenti di trasformazione integrati, ma a volte serve una logica più avanzata. La funzione apply ti permette di applicare una funzione definita dall’utente a una riga o a una colonna di un DataFrame, aprendo la strada a trasformazioni avanzate e generazione di feature.
La funzione find_street_name() estrae il nome della via da "street_address", rimuovendo il numero civico dalla stringa. Questa funzione è stata caricata in memoria ed è pronta per essere applicata al DataFrame raw_testing_scores.
Questo esercizio fa parte del corso
ETL and ELT in Python
Istruzioni dell'esercizio
- Nella definizione della funzione
transform(), usa la funzionefind_street_name()per creare una nuova colonna chiamata"street_name". - Usa la funzione
transform()per ripulire il DataFrameraw_testing_scores. - Stampa l’head del DataFrame
cleaned_testing_scores, verificando la nuova colonna"street_name".
Esercizio pratico interattivo
Prova a risolvere questo esercizio completando il codice di esempio.
def transform(raw_data):
# Use the apply function to extract the street_name from the street_address
raw_data["street_name"] = raw_data.____(
# Pass the correct function to the apply method
____,
axis=1
)
return raw_data
# Transform the raw_testing_scores DataFrame
cleaned_testing_scores = ____(raw_testing_scores)
# Print the head of the cleaned_testing_scores DataFrame
print(cleaned_testing_scores.____())