【发布时间】:2016-08-20 19:57:42
【问题描述】:
我已经阅读了各种解决方案,并尝试了此处所述的解决方案:Pandas: Converting to numeric, creating NaNs when necessary
但这并没有真正解决我的问题:
我有一个包含多列的数据框,其中一列 ['PricePerSeat_Outdoor'] 包含一些浮点值、一些空值和一些 '-'
print type(df_raw['PricePerSeat_Outdoor'][99])
print df_raw['PricePerSeat_Outdoor'][95:101]
df_raw['PricePerSeat_Outdoor'] = df_raw['PricePerSeat_Outdoor'].apply(pd.to_numeric, errors='coerce')
print type(df_raw['PricePerSeat_Outdoor'][99])
然后我得到:
<type 'str'>
95 17.21
96 17.24
97 -
98 -
99 17.2
100 17.24
Name: PricePerSeat_Outdoor, dtype: object
<type 'str'>
第 98 行和第 99 行的值未转换。同样,我已经尝试了多种方法,包括以下方法,但它没有奏效。如果有人能给我一些提示,不胜感激。
df_raw['PricePerSeat_Outdoor'] = df_raw['PricePerSeat_Outdoor'].apply(pd.to_numeric, errors='coerce')
另外,如何一次将多列转换为数字?谢谢。
【问题讨论】:
标签: python pandas dataframe typeconverter