I am having difficulties with setting some variables to numeric. (I have just started to learn python for data science and have minimal background.)
I tried those:
data["S2BQ1A25"] = data["S2BQ1A25"].convert_objects(convert_numeric=True)
df[["S2BQ1A16", "S2BQ1A25"]] = df[["S2BQ1A16", "S2BQ1A25"]].apply(pd.to_numeric)
data["S2BQ1A16"] = pandas.to_numeric(data["S2BQ1A16"] )
data["S2BQ1A16"] = pd.to_numeric(data["S2BQ1A16"])
I am using Anaconda, Spyder to code. Python 3.8. I imported pandas (1.0.5) and numpy(1.18.5).
Thanks in advance.
Edit: For S2BQ1A16 and S2BQ1A25 there were 4 choices: 1 yes, 2 no, 9 unknown, BL. NA, lifetime abstainer.
Errors I got, respectively:
File "/home/nida/Desktop/p-projects/temp.py", line 18, in <module>
data["S2BQ1A25"] = data["S2BQ1A25"].convert_objects(convert_numeric=True)
File "/home/nida/anaconda3/lib/python3.8/site-packages/pandas/core/generic.py", line 5274, in __getattr__
return object.__getattribute__(self, name)
AttributeError: 'Series' object has no attribute 'convert_objects'
File "/home/nida/Desktop/p-projects/temp.py", line 18, in <module>
df[["S2BQ1A16", "S2BQ1A25"]] = df[["S2BQ1A16", "S2BQ1A25"]].apply(pd.to_numeric)
NameError: name 'df' is not defined
File "/home/nida/Desktop/p-projects/temp.py", line 18, in <module>
data["S2BQ1A16"] = pandas.to_numeric(data["S2BQ1A16"] )
File "/home/nida/anaconda3/lib/python3.8/site-packages/pandas/core/tools/numeric.py", line 149, in to_numeric
values = lib.maybe_convert_numeric(
File "pandas/_libs/lib.pyx", line 1963, in pandas._libs.lib.maybe_convert_numeric
ValueError: Unable to parse string " " at position 0
File "/home/nida/Desktop/p-projects/temp.py", line 18, in <module>
data["S2BQ1A16"] = pd.to_numeric(data["S2BQ1A16"])
NameError: name 'pd' is not defined