Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (18.4k points)

I have the pandas data frame with a column designated to town names. After each town name, I am adding a word "NSW" (e.g. "Sydney" will become "Sydney NSW"). However, this means even when a town already has NSW written, the script will add it again (e.g. "Narara NSW" will become "Narara NSW NSW"). How can I check if a name already has NSW and only add a string if NSW is not present? Here is my code so far:

#Adds "NSW" to the end of each town in the data frame and then adds these changes to to a csv

df['FullAddress'] = df['FullAddress'] + ' NSW'

print(df)

df.to_csv('latLongTest.csv', index=False)

1 Answer

0 votes
by (36.8k points)

My personal preference is to usually use np.where() in a situation like this:

df['FullAddress'] = np.where((df['FullAddress'].str.endswith(' NSW')), df['FullAddress'], df['FullAddress'] + ' NSW')

It is a vectorized and similar to an excel if statement IF(CONDITION, THEN, ELSE).

If you want to know more about the Data Science then do check out the following Data Science which will help you in understanding Data Science from scratch

Browse Categories

...