137-16 Data Cleaning Walkthrough: Combining the Data

UNABLE TO UNDERSTAND THE WORKING OF FUNCTION HERE
Screen Link:

My Code:
def get_first_two_chars(dbn):
return dbn[0:2]

combined[“school_dist”] = combined[“DBN”].apply(get_first_two_chars)

Replace this line with your code

What I expected to happen:

What actually happened:

Replace this line with the output/error

The get_first_two_chars function takes in an argument, which I’m assuming is a string and return the first two characters of the string. For example, if it takes in the string "dataquest" it would return "da".

The apply method is used to apply a function to an entire series at once. You applied the get_first_two_chars function to the combined[“DBN”] series, so it returns the first two characters of each element of the series. Finally, you assigned these elements to combined[“school_dist”] . Therefore each element of the school_dist column of the combined dataframe is made by the first two characters of the DBN column.

You can read more about the apply method here.