Skip to content Skip to sidebar Skip to footer

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

Solution 1:

One way would be to do:

In [212]: df
Out[212]: 
  Column1  Column2 Column3                   Column4   Column5
0     5FQ    1.047  S$55.3  UG44.2as 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
05.01.04755.344.28.2

Solution 2:

You can use pd.Series.extract:

In [28]: for c indf:
    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

Note that this is a bit brittle, as in Column4 it works because the date appeared after the quantity. Your question doesn't specify anything more precise, though.

Post a Comment for "Pandas Dropping Of Words In Dataframe"