Quite rightly so, for one very simple reason: how would you change any of those text values (e.g.
white) into a meaningful numerical value? You can’t, because such an operation is nonsense.
If you read the documentation for the
to_numeric() method, you’ll see that there is an optional parameter
errors which defaults to
raise. This means that when you provide it with data it cannot parse to a number, it raises an exception - this is the behaviour you’re seeing. Changing your code to
us_census.race = pd.to_numeric(us_census.race, errors='coerce') will make pandas return
NaN for any data which it cannot parse to a number.
What the value of this would be is dubious, though - because the entire list you have in
race would evaluate to
NaN, so not sure how that’ll improve what you’re trying to do.
I am not hugely familiar with pandas or matplotlib, but I don’t need to be to see that your code is wrong. You’ve got:
for race in us_census:
race = ['Hispanic', 'White', 'Black', 'Native', 'Asian', 'Pacific']
race as the placeholder variable for your iterator, then immediately assigning it the full list once you’re in the loop… which, again quite rightly, is throwing an error. As to whether you’re doing the plotting correctly? I don’t know, as I don’t know that much about how to use matplotlib.
Lastly, whilst perhaps not so urgent in this case as the issues are reasonably obvious, it generally helps to provide the complete text of any error messages your code is throwing rather than simply stating that you got an error message. The Traceback in Python is pretty helpful at tracking down what’s causing your code to fail.