-1

I'm looking at a table of mock Yelp reviews (data_clean is my pandas.dataframe). My column 'stars' is mixed text and integers. I want to convert the text values to integers. I'm trying the below. It doesn't error but also doesn't work!

data_clean.stars.replace([type(data_clean.stars)=='string'],[data_clean['stars'].astype(int)])

Help appreciated!

  • Welcome to StackOverflow. Please read about how to ask good questions (link below). You will want to give us enough detail to know what you're talking about -- what does your data look like, what result do you want, what are you getting with what you've tried -- if you hope to get a useful answer. See here: http://stackoverflow.com/help/how-to-ask – itzy Dec 17 '15 at 19:11
  • For starters, `type(...)` is never going to be a string called `'string'`. http://stackoverflow.com/questions/152580/whats-the-canonical-way-to-check-for-type-in-python – itzy Dec 17 '15 at 19:12

1 Answers1

0

Try:

 data_clean['stars'] = [int(x) for x in data_clean['stars']]

This will fail if you have any element that fails int(element), e.g strings that are not digits, in that case you should use some kind of try and except strategy depending on what exactly you want to do with these elements.

Ezer K
  • 3,637
  • 3
  • 18
  • 34