Python 数据帧中单词的删除

Python 数据帧中单词的删除,python,regex,dataframe,Python,Regex,Dataframe,我有一个示例数据框,我希望删除所有单词并保留值 Column1 Column2 Column3 Column4 Column5 5FQ 1.047 S$55.3 UG44.2 as of 02/Jun/2016 S$8.2 mm 有没有可能丢下单词,保留所有的数字?IE:要获得以下所需结果: Column1 Column2 Column3 Column4 Column5 5

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

Column1    Column2    Column3    Column4                     Column5
5FQ        1.047      S$55.3     UG44.2 as of 02/Jun/2016    S$8.2 mm
有没有可能丢下单词,保留所有的数字?IE:要获得以下所需结果:

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

一种方法是:

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
您可以使用:

请注意,这有点脆弱,因为在
第4列中,日期出现在数量之后,所以它起作用。不过,您的问题没有更精确的说明。

您可以使用:


请注意,这有点脆弱,因为在
第4列中,日期出现在数量之后,所以它起作用。不过,您的问题没有具体说明更精确的内容。

您想从
S$55.3
中删除
S
,而不是从
S$8.2 mm
中删除吗?我看不出模式@jakewong和“5FQ”不仅仅变成了“5”?那么产生的
5FQ
-为什么不变成
5
?你想从
S$55.3
中删除
S
,而不是从
S$8.2mm
中删除?我看不出模式@jakewong和“5FQ”不仅仅变成了“5”?那么产生的
5FQ
-为什么不变成
5
?谢谢Nehal。只是好奇。例如,如果在
Column2
中,值是
(1.047)
,是否可以将其转换为
-1.047
,而不仅仅是
1.047
。只是好奇。例如,如果在
Column2
中,值是
(1.047)
,是否可以将其转换为
-1.047
,而不仅仅是
1.047