I.Villar
I.Villar

Reputation: 43

Str.replace in python doesn't seem to work

my problem is pretty straightforward. the column names of my dataframe include their source but i'd like to take it out to make it simpler.

df.columns brings back

 Index(['a_(Source:_WaPo)','b_(Source:_WaPo)','c_(Source:_WaPo)'],
      dtype='object')

so what I did was

df.columns = df.columns.str.replace('_(Source:_WaPo)','')

self assignment didn't seem to do anything (i'm working in jupyter notebook) so I tried just doing it without the self assignment to see what it'd look like

df.columns.str.replace('_(Source:_WaPo)','')

but the columns didn't change at all.

Is there an alternate way of doing this? or am I doing it wrong in any way? any help would be appreciated.

This is just an example, so let's say I have a lot of columns, and I wouldn't want to just manually replace each of them.

Upvotes: 0

Views: 76

Answers (2)

Sohaib Anwaar
Sohaib Anwaar

Reputation: 1547

Actually from df.columns you got list and list does have function have replace. Instead you have to iterate over the list in which your column names are present where you can easily replace string.

df.columns = [column.replace("_(Source:_WaPo","") for column in df.columns]

Upvotes: 0

Nilesh Ingle
Nilesh Ingle

Reputation: 1883

Code below replaces the text:

df.columns.str.replace(r"_\(Source:_WaPo\)","")

or

df.columns = [x.replace("_(Source:_WaPo)","") for x in df.columns]

Upvotes: 1

Related Questions