您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

python – 将pandas dataframe中的列从String转换为Float

5b51 2022/1/14 8:20:52 python 字数 3240 阅读 491 来源 www.jb51.cc/python

我已经阅读了各种解决方案,并尝试了此处所述的解决方案:Pandas: Converting to numeric, creating NaNs when necessary但它并没有真正解决我的问题:我有一个数据框包含多个列,其中列['PricePerSeat_Outdoor']包含一些浮点值,一些空值和一些' - ' print type(df_ra

概述

我已经阅读了各种解决方案,并尝试了此处所述的解决方案: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]) 

然后我得到了:


  

第98行和第99行的值未转换.同样,我已经尝试过多种方法,包括以下但是它没有用.非常感谢,如果有人能给我一些提示.

df_raw [‘PricePerSeat_Outdoor’] = df_raw [‘PricePerSeat_Outdoor’].apply(pd.to_numeric,errors =’coerce’)

另外,如何将多个列一次转换为数字?谢谢.

df_raw['PricePerSeat_Outdoor'] = pd.to_numeric(df_raw['PricePerSeat_Outdoor'],errors='coerce')

这是一个例子:

In [97]: a = pd.Series(['17.21','17.34','15.23','-','','12.34']

In [98]: b = pd.Series(['0.21','0.34','0.23','0.34'])

In [99]: df = pd.DataFrame({'a':a,'b':b})

In [100]: df['c'] = np.random.choice(['a','b','b'],len(df))

In [101]: df
Out[101]:
       a     b  c
0  17.21  0.21  a
1  17.34  0.34  b
2  15.23  0.23  b
3      -     -  b
4      -        b
5            -  b
6  12.34  0.34  b

In [102]: cols_to_convert = ['a','b']

In [103]: cols_to_convert
Out[103]: ['a','b']

In [104]: for col in cols_to_convert:
   .....:         df[col] = pd.to_numeric(df[col],errors='coerce')
   .....:

In [105]: df
Out[105]:
       a     b  c
0  17.21  0.21  a
1  17.34  0.34  b
2  15.23  0.23  b
3    NaN   NaN  b
4    NaN   NaN  b
5    NaN   NaN  b
6  12.34  0.34  b

校验:

In [106]: df.dtypes
Out[106]:
a    float64
b    float64
c     object
dtype: object

总结

以上是编程之家为你收集整理的python – 将pandas dataframe中的列从String转换为Float全部内容,希望文章能够帮你解决python – 将pandas dataframe中的列从String转换为Float所遇到的程序开发问题。


如果您也喜欢它,动动您的小指点个赞吧

除非注明,文章均由 laddyq.com 整理发布,欢迎转载。

转载请注明:
链接:http://laddyq.com
来源:laddyq.com
著作权归作者所有。商业转载请联系作者获得授权,非商业转载请注明出处。


联系我
置顶