I have a Pandas dataframe like this:
Country Ranking 0 Greece[e] 100 1 China[e] 200 2 Singapore 300 3 Australia[e] 400 4 Zambia 500 5 France 600 6 Canada[e] 700
I like to remove any ‘[e]’ under ‘Country’ column of the DF, to have this:
Country Ranking 0 Greece 100 1 China 200 2 Singapore 300 3 Australia 400 4 Zambia 500 5 France 600 6 Canada 700
When I use:
df.Country = df.Country.str.strip('[e]')
I get the ‘[e]’ is removed including the last ‘e’ of country’s name (e.g. Singapore, Greece, France).
Country Ranking 0 Greec 100 1 China 200 2 Singapor 300 3 Australia 400 4 Zambia 500 5 Franc 600 6 Canada 700
Advertisement
Answer
.str.replace()
:
df["Country"] = df["Country"].str.replace(r"[e]$", "") print(df)
Prints:
Country Ranking 0 Greece 100 1 China 200 2 Singapore 300 3 Australia 400 4 Zambia 500 5 France 600 6 Canada 700