Home > database >  PYTHON - .replace function
PYTHON - .replace function

Time:11-12

I have a DF similar to the below:

Name Text
Michael 66l additional text
John 55i additional text
Mary 88l additional text

What I want to do is anywhere "l" occurs in the first string of the "Text" column, then replace it with "P"

Current code

DF['Text'] = DF['Text'].replace({"l", "P", 1})

Desired Outcome

Name Text
Michael 66P additional text
John 55i additional text
Mary 88P additional text

CodePudding user response:

You can use pandas.Series.str.replace with regex to identify the first word of the string.

>>> import pandas as pd
>>>
>>>
>>> df
                  Text
0  66l additional text
1  55i additional text
2  88l additional text
>>>
>>>
>>> df['Text'] = df['Text'].str.replace(r"^\w \b", lambda x: x.group(0).replace("l", "P"), regex=True)
>>> df
                  Text
0  66P additional text
1  55i additional text
2  88P additional text

CodePudding user response:

Asssuming the l only occurs once (as is shown in your sample dataframe) you can use

df['Text'].str.replace(r'^(\S*)l', r'\1P', regex=True)
# => 0    66P additional text
#    1    55i additional text
#    2    88P additional text
#    Name: Text, dtype: object

See the regex demo. Details:

  • ^ - start of string
  • (\S*) - Group 1: zero or more whitespaces
  • l - an l char (letter).

The replacement is \1P, i.e. the Group 1 value P letter.

CodePudding user response:

With your shown samples only, this could be easily done by using str[range] functionality of Python pandas, with your shown samples of DataFrame please try following code.

import pandas as pd
##Create your df here....
df['Text'] = df['Text'].str[:2]   'P '   df['Text'].str[4:]

Explanation:

  • df['Text'].str[:2]: Taking(printing) from 1st position of column Text to till 3rd position(it starts from 0).
  • 'P ' : Adding/concatenating P to it as per OP's requirement in question here.
  • df['Text'].str[4:]: Taking(printing) from 5th position of column Text to till end of column's value here and saving this whole df['Text'].str[:2] 'P ' df['Text'].str[4:] code's output into Text column itself of DataFrame.
  • Related