Hi! could someone help me with it? Any problem with my function code? The output is not what I expected. Thank you!!!
Screen Link: https://app.dataquest.io/m/137/data-cleaning-walkthrough%3A-combining-the-data/16/adding-a-school-district-column-for-mapping
My Code:
def get_first_2_C(col):
for v in col:
first_2= v[0:2]
return first_2
combined['school_dist']=combined['DBN'].apply(get_first_2_C)
print(combined['school_dist'].head())
What I expected to happen:
Take first 2 letter in each value of “DBN” column
What actually happened:
0 2
1 8
2 0
3 9
4 9
Name: school_dist, dtype: object
When you use Series.apply()
you 're applying a function element-wise in the Series, so you do not need to use the for
, because what the function receives is an element of the Series. When you use the for
like you did you are in fact looping through the element itself, not through the column. This how the code should look like:
def get_first_2_C(v):
first_2= v[0:2]
return first_2
combined['school_dist']=combined['DBN'].apply(get_first_2_C)
print(combined['school_dist'].head())