jake wong jake wong - 3 months ago 9
Python Question

pandas dropping of words in dataframe

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


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

Answer

One way would be to do:

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