Pandas delete words in dataframe

I have a sample data frame that I want to discard all words and save values.

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

Can I reset words and keep all numbers? IE: to get the desired results below:

Column1    Column2    Column3    Column4    Column5
5          1.047      55.3       44.2       8.2
+1
source share
2 answers

One of the methods:

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
+3
source

You can use pd.Series.extract:

In [28]: for c in df:
    df[c] = df[c].str.extract('(\d+\.?\d*)', expand=False)
   ....:     

In [29]: df
Out[29]: 
  Column1 Column2 Column3 Column4 Column5
0       5   1.047    55.3    44.2     8.2

Please note that this is a bit fragile, as Column4it works because the date appeared after the quantity. However, your question does not indicate anything more accurate.

+3
source

Source: https://habr.com/ru/post/1653570/


All Articles