user71812
user71812

Reputation: 457

Replacing values in strings in Python using replace method doesn't work

I am using the dataset from Kaggle: https://www.kaggle.com/rajeevw/ufcdata with the data.csv file, and I tried to replace the values from Weight column using this code to later transform it to integer column, but I am already stuck at getting rid of the strings as follows:

fighter_data['Weight'] = fighter_data['Weight'].replace(" lbs.",'')
fighter_data

So an example will be: a value of 145 lbs. and I use the code above to get rid of lbs so that it can be integer 145. Apparently the code above doesn't work and nothing changes. What did I do wrong? Thanks!

Upvotes: 2

Views: 85

Answers (2)

Equan Ur Rehman
Equan Ur Rehman

Reputation: 269

try

fighter_data['Weight'] = fighter_data['Weight'].str.replace(" lbs.",'')

Upvotes: 2

U13-Forward
U13-Forward

Reputation: 71560

Try using regex=False and str.replace:

fighter_data['Weight'] = fighter_data['Weight'].str.replace(" lbs.",'', regex=False)

Or use \\:

fighter_data['Weight'] = fighter_data['Weight'].str.replace(" lbs\\.",'')

And add a .astype(int) at the end of the line if you want to convert to integer.

Upvotes: 4

Related Questions