Python Practice › Pandas Programs › Program 78
Keep only words
Write a pandas program to remove the numbers from an address column and keep the words.
Output
Name Address words 0 Ramesh 297 shukrawar peth shukrawar peth 1 Suresh 200 ravivar peth ravivar peth 2 Sanket 090 shanivar peth shanivar peth
Approach
str.replace(r"\d+", "")then strip.
Solution
import pandas as pddf = pd.DataFrame({"Name": ["Ramesh", "Suresh", "Sanket"], "Address": ["297 shukrawar peth", "200 ravivar peth", "090 shanivar peth"]})df["words"] = df["Address"].str.replace(r"\d+", "", regex=True).str.strip()print(df)Output
Name Address words 0 Ramesh 297 shukrawar peth shukrawar peth 1 Suresh 200 ravivar peth ravivar peth 2 Sanket 090 shanivar peth shanivar peth
Learn the concept
New to this topic? Read the lesson, then come back.