问题描述
我正在转圈,尝试了许多不同的方式,所以我猜我的核心理解是错误的.感谢您在理解编码/解码问题方面的帮助.
I am going around in circles and tried so many different ways so I guess my core understanding is wrong. I would be grateful for help in understanding my encoding/decoding issues.
我从SQL导入数据框,似乎某些数据类型:float64被转换为Object.因此,我无法进行任何计算.我无法将对象转换回float64.
I import the dataframe from SQL and it seems that some datatypes:float64 are converted to Object. Thus, I cannot do any calculation. I fail to convert the Object back to float64.
df.head()
Date WD Manpower 2nd CTR 2ndU T1 T2 T3 T4
2013/4/6 6 NaN 2,645 5.27% 0.29 407 533 454 368
2013/4/7 7 NaN 2,118 5.89% 0.31 257 659 583 369
2013/4/13 6 NaN 2,470 5.38% 0.29 354 531 473 383
2013/4/14 7 NaN 2,033 6.77% 0.37 396 748 681 458
2013/4/20 6 NaN 2,690 5.38% 0.29 361 528 541 381
df.dtypes
WD float64
Manpower float64
2nd object
CTR object
2ndU float64
T1 object
T2 object
T3 object
T4 object
T5 object
dtype: object
SQL表:
推荐答案
您只需调用 convert_objects
:
You can convert most of the columns by just calling convert_objects
:
In [36]:
df = df.convert_objects(convert_numeric=True)
df.dtypes
Out[36]:
Date object
WD int64
Manpower float64
2nd object
CTR object
2ndU float64
T1 int64
T2 int64
T3 int64
T4 float64
dtype: object
对于列第二"和点击率",我们可以将向量化的 str
方法来替换千位分隔符并删除'%'符号,然后 astype
进行转换:
For column '2nd' and 'CTR' we can call the vectorised str
methods to replace the thousands separator and remove the '%' sign and then astype
to convert:
In [39]:
df['2nd'] = df['2nd'].str.replace(',','').astype(int)
df['CTR'] = df['CTR'].str.replace('%','').astype(np.float64)
df.dtypes
Out[39]:
Date object
WD int64
Manpower float64
2nd int32
CTR float64
2ndU float64
T1 int64
T2 int64
T3 int64
T4 object
dtype: object
In [40]:
df.head()
Out[40]:
Date WD Manpower 2nd CTR 2ndU T1 T2 T3 T4
0 2013/4/6 6 NaN 2645 5.27 0.29 407 533 454 368
1 2013/4/7 7 NaN 2118 5.89 0.31 257 659 583 369
2 2013/4/13 6 NaN 2470 5.38 0.29 354 531 473 383
3 2013/4/14 7 NaN 2033 6.77 0.37 396 748 681 458
4 2013/4/20 6 NaN 2690 5.38 0.29 361 528 541 381
或者您可以在不调用astype
的情况下执行上述字符串处理操作,然后调用convert_objects
一次转换所有内容.
Or you can do the string handling operations above without the call to astype
and then call convert_objects
to convert everything in one go.
更新
由于版本0.17.0
convert_objects
已被弃用,并且没有顶级功能可以执行此操作,因此您需要执行以下操作:
Since version 0.17.0
convert_objects
is deprecated and there isn't a top-level function to do this so you need to do:
df.apply(lambda col:pd.to_numeric(col, errors='coerce'))
请参见 docs 和以下相关问题: pandas:to_numeric用于多列
See the docs and this related question: pandas: to_numeric for multiple columns
这篇关于如何在python中将datatype:object转换为float64?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!