2

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!

1
  • You could also use regex for this eh, it might be a bit cleaner. Commented Nov 13, 2019 at 4:36

2 Answers 2

4

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.

Sign up to request clarification or add additional context in comments.

4 Comments

Thank you! This is perfect! I have no idea we have to put .str in between :(
I can only do so two minutes after this, btw do you know how to get rid of ' or " character? I tried to do so with the above .str.replace() but it doesn't work.
@user71812 fighter_data['Height'] = fighter_data['Height'].astype(str).str.replace("'",'.').str.replace('"','').astype(float)
Yay this works! Thanks a lot @U10-Forward for your great help! Have a great day!
2

try

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

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.