无法将pandas字符串转换为数字



我有以下输入数据帧。所有类型都是字符串。我想将它们转换为浮动。有各种各样的字符,但理想情况下,我希望只保留十进制分隔符和数字。删除其他所有内容的最佳方法是什么?我试过了:

corp = corp.replace(r'$', '', regex=True).apply(pd.to_numeric)

有办法更换all expect numeric AND comma吗?

JPY             JPY           JPY       JPY             JPY             JPY       JPY           JPY          JPY     JPY  ... JPY JPY JPY JPY JPY JPY JPY JPY JPY JPY
Update time                                                                                                                                              ...                                        
2018/8/13 10:15     $34,424,234.98  this is a str   ¥375,567,698  ¥304,734  ¥3,848,230,263    ¥101,677,219         0   ¥14,377,274  ¥47,719,464  ¥1,833  ...   0   0   0   0   0   0   0   0   0   0
2018/8/14 10:30     $34,424,234.98  ¥4,079,039,244  ¥375,567,698  ¥304,734  ¥3,131,351,753    ¥101,677,219         0   ¥14,377,274  ¥47,719,464  ¥1,833  ...   0   0   0   0   0   0   0   0   0   0
2018/8/15 10:30     $34,424,234.98  ¥4,644,436,742  ¥375,567,698  ¥304,734  ¥3,018,288,133    ¥101,677,219         0   ¥14,376,734  ¥48,551,464  ¥1,833  ...   0   0   0   0   0   0   0   0   0   0

编辑:

这是一个解决方案。。。

corp = corp.replace(r'[a-zA-Z]|¥|,', '', regex=True)

您可以使用

corp = corp.replace(r'[^d.]+', '', regex=True).apply(pd.to_numeric)

通过这种方式,您将删除除数字和点之外的所有字符。请参阅regex演示。

详细信息

  • [^-否定字符类的开始
    • d-数字
    • .-点
  • ]+-否定字符类结束,一次或多次出现

最新更新