Home > Enterprise >  Changing the DataFrame using a regular expression
Changing the DataFrame using a regular expression

Time:09-15

I just started studying pandas and I had a question: I need to use a regular expression to find strings of a certain pattern and replace the entire string with this pattern. I used :

df.replace(to_replace=r'^[Gg]eramny', value='Berlin', regex=True) 

Source string: 'Platz der Republik 1, 10557 Berlin, germany'

My code outputs: 'Platz der Republik 1, 10557 Berlin, Berlin'

But I need the line: 'Berlin'

CodePudding user response:

consider, result = d.replace(r'[Gg]ermany','Berlin',regex=true) this command through we cant use regular expression. error will become,The error was caused because we passed keyword arguments to the replace() method.

The replace() method takes only positional arguments. so we need import re for regex

import pandas as pd
import re                   

d='Platz der Republik 1, 10557 Berlin, germany'
print(d)


result = re.sub(r'[Gg]ermany','Berlin',d)
print(result)

Output : enter image description here

HOPE IT HELP YOU

CodePudding user response:

Consider new data have multiple string under array so we will do exactly similar but just show data type of dataframe.

import pandas as pd
import re                   

d={'Array':['Platz der Republik 1, 10557 Berlin, germany','Platz der Republik 1, 10557 Berlin, germany','Platz der Republik 1, 10557 Berlin, germany']}

#create Dataframe
dd = pd.DataFrame(d)
print(dd)

result = re.sub(r'[Gg]ermany','Berlin',str(dd))
print(result)
  • Related