Intellipaat 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 the 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 the town already has NSW written, a 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 the 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 the 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 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

31k questions

32.8k answers

501 comments

693 users

Browse Categories

...