Python 将数据帧转换为浮点
我无法将数据帧中的数据类型转换为字符串浮点(它们是字符串或空字符串形式的数值):Python 将数据帧转换为浮点,python,python-3.x,pandas,dataframe,Python,Python 3.x,Pandas,Dataframe,我无法将数据帧中的数据类型转换为字符串浮点(它们是字符串或空字符串形式的数值): 我做错了什么?假设数据帧: df = pd.DataFrame({'a':['5','10'], 'b':['9','7']}) 当前数据类型: In [391]: df.dtypes Out[391]: a object b object 将整个df列转换为数字: df = df.apply(pd.to_numeric) In [396]: df = pd.DataFrame({'a':['5
我做错了什么?假设数据帧:
df = pd.DataFrame({'a':['5','10'], 'b':['9','7']})
当前数据类型:
In [391]: df.dtypes
Out[391]:
a object
b object
将整个df
列转换为数字:
df = df.apply(pd.to_numeric)
In [396]: df = pd.DataFrame({'a':['5','10'], 'b':['9','7']})
In [397]: df['a'] = df['a'].apply(pd.to_numeric)
In [398]: df.dtypes
Out[398]:
a int64
b object
结果:
In [393]: df.dtypes
Out[393]:
a int64
b int64
仅将选定列转换为数字:
df = df.apply(pd.to_numeric)
In [396]: df = pd.DataFrame({'a':['5','10'], 'b':['9','7']})
In [397]: df['a'] = df['a'].apply(pd.to_numeric)
In [398]: df.dtypes
Out[398]:
a int64
b object
to_numeric
不起作用,您需要将其返回值分配到某个位置