1

I'm trying to change the Strings "SLL" under the competitions column to "League" but when i tried this:

messi_dataset.replace("SLL", "League",regex = True)

It only changed the first "SLL" to "League" but then other strings that were "SLL" became "UCL. I have no idea why. I also tried changing regex = True to inlace = True but no luck.

https://drive.google.com/file/d/1ldq6o70j-FsjX832GbYq24jzeR0IwlEs/view?usp=sharing

https://drive.google.com/file/d/1OeCSutkfdHdroCmTEG9KqnYypso3bwDm/view?usp=sharing

Yevhen Kuzmovych
  • 10,940
  • 7
  • 28
  • 48
George Zambrano
  • 47
  • 3
  • 11

2 Answers2

0

Suppose you have a dataframe as below:

import pandas as pd
import re
df = pd.DataFrame({'Competitions': ['SLL', 'sll','apple', 'banana', 'aabbSLL', 'ccddSLL']})

# write a regex pattern that replaces 'SLL' 
# I assumed case-irrelevant

regex_pat = re.compile(r'SLL', flags=re.IGNORECASE)
df['Competitions'].str.replace(regex_pat, 'league', regex=True)

#   Input DataFrame
    Competitions
0   SLL
1   sll
2   apple
3   banana
4   aabbSLL
5   ccddSLL

Output:

0        league
1        league
2         apple
3        banana
4    aabbleague
5    ccddleague
Name: Competitions, dtype: object

Hope it clarifies.

Priya
  • 723
  • 1
  • 5
  • 7
0

base on this Answer test this code:

messi_dataset['competitions'] = messi_dataset['competitions'].replace("SLL", "League")

also, there are many different ways to do this like this one that I test:

messi_dataset.replace({'competitions': 'SLL'}, "League")

for those cases that 'SLL' is a part of another word:

messi_dataset.replace({'competitions': 'SLL'}, "League", regex=True)
zana saedpanah
  • 324
  • 3
  • 12