pandas 在数据框中的单词掉落 [英] pandas dropping of words in dataframe

查看:51
本文介绍了 pandas 在数据框中的单词掉落的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有一个示例数据框,希望删除所有单词并保留值。

I have a sample dataframe that I wish to drop all words and keep the values.

Column1    Column2    Column3    Column4                     Column5
5FQ        1.047      S$55.3     UG44.2 as of 02/Jun/2016    S$8.2 mm

是否可以丢弃单词并保留所有数字? IE:要在下面获得理想的结果:

Is it possible to drop words and keep all the numbers? IE: to get the desired results below:

Column1    Column2    Column3    Column4    Column5
5          1.047      55.3       44.2       8.2


推荐答案

一种方法是:

In [212]: df
Out[212]: 
  Column1  Column2 Column3                   Column4   Column5
0     5FQ    1.047  S$55.3  UG44.2 as of 02/Jun/2016  S$8.2 mm

In [213]: df.apply(lambda x: x.astype(str).str.extract(r'(\d+\.?\d*)', expand=True).astype(np.float))
Out[213]: 
   Column1  Column2  Column3  Column4  Column5
0      5.0    1.047     55.3     44.2      8.2

这篇关于 pandas 在数据框中的单词掉落的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆