I have a DataFrame that contains numbers as strings with commas for the thousands marker. I need to convert them to floats.
a = [['1,200', '4,200'], ['7,000', '-0.03'], [ '5', '0']]
df=pandas.DataFrame(a)
I am guessing I need to use locale.atof. Indeed
df[0].apply(locale.atof)
works as expected. I get a Series of floats.
But when I apply it to the DataFrame, I get an error.
df.apply(locale.atof)
TypeError: ("cannot convert the series to ", u'occurred at index 0')
and
df[0:1].apply(locale.atof)
gives another error:
ValueError: ('invalid literal for float(): 1,200', u'occurred at index 0')
So, how do I convert this DataFrame
of strings to a DataFrame of floats?
Question&Answers:
os 与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…