Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

replace string in pandas dataframe

I have a dataframe with multiple columns. I want to look at one column and if any of the strings in the column contain @, I want to replace them with another string. How would I go about doing this?

like image 811
DJK Avatar asked Jul 20 '26 05:07

DJK


1 Answers

A dataframe in pandas is composed of columns which are series - Panda docs link

I'm going to use regex, because it's useful and everyone needs practice, myself included! Panda docs for text manipulation

Note the str.replace. The regex string you want is this (it worked for me): '.*@+.*' which says "any character (.) zero or more times (*), followed by an @ 1 or more times (+) followed by any character (.) zero or more times (*)

df['column'] = df['column'].str.replace('.*@+.*', 'replacement')

Should work, where 'replacement' is whatever string you want to put in.

like image 188
Bill Harper Avatar answered Jul 22 '26 17:07

Bill Harper