Difference between str(Series).split() and Series.str.split()

Screen Link: https://app.dataquest.io/m/346/working-with-strings-in-pandas/2/using-apply-to-transform-strings

Your Code: ```def extract_last_word(element):
return element.str.split()[-1]
merged[‘Currency Apply’] = merged[‘CurrencyUnit’].apply(extract_last_word)


What I expected to happen: To split the string values for the Series 'element' in the function i.e for the merged['CurrencyUnit'] Series.

What actually happened: Error message:  ```AttributeError: 'str' object has no attribute 'str'``` 

Other details: On running merged['CurrencyUnit'].str.split() it perfectly splits the values into a list. But gives error in the map() function.

Hey there!

When you apply a function to a Series, it automatically iterates through each element in the Series — this means that you’re passing a string into the function for each iteration through the Series.

str.split() is vectorized version of Python’s built-in split() function (https://docs.python.org/2/library/stdtypes.html#str.split) that only works for Series and DataFrames. Since you’re not passing in a Series, but individual strings, you’d want to omit using the str. accessor and just use the .split() function directly on the string.

Let me know if that helps!

2 Likes