I have a dataframe with a column that contains street intersections
| Locations |
--------------------------------
|W Madison Ave & S Randall Blvd|
|N Clemson St & E Tower Ave |
|E Thompson St & S Garfield Ln |
I'd like to remove the directional characters (N, S, E, W) as well as the suffixes of the streets (Blvd, St, Ave, etc...) so that my output looks like this
| Locations |
---------------------
|Madison & Randall |
|Clemson & Tower |
|Thompson & Garfield|
I can't do a str.replace() because it would be removing characters from the words I need to stay. I tried using lstrip() and rstrip() but that wouldn't fix the characters I'd like removed from the middle of the string.
I also tried experimenting with Series.apply()
banned = ['N', 'S', 'E', 'W', 'Ave', 'Blvd', 'St', 'Ln']
df["Locations"].apply(lambda x: [item for item in x if item not in banned])
But this essentially does a str.replace() and places everything in a list in the dataframe instead.
You are close - you can split values first and then join:
f = lambda x: ' '.join([item for item in x.split() if item not in banned])
df["Locations"] = df["Locations"].apply(f)
Or list comprehension:
df["Locations"] = [' '.join([item for item in x.split()
if item not in banned])
for x in df["Locations"]]
print (df)
Locations
0 Madison & Randall
1 Clemson & Tower
2 Thompson & Garfield
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With