EVS_93
EVS_93

Reputation: 191

Substitute all floats for a single character in pandas

I have a dataframe with mostly dates as columns.

Ex:

  Names 01/04/2020 02/04/2020...
0 Name1        1.0        NaN
1 Name2        2.0        8.0
...

I want to substitute all these floats for the character 'P'

Ex:

  Names 01/04/2020 02/04/2020...
0 Name1          P        NaN
1 Name2          P          P
...

I've tried using iloc, but didn't work:

df.iloc[:,1:][df.iloc[:, 1:] >=1] = 'P'

Any ideas?

Upvotes: 2

Views: 283

Answers (4)

Shubham Gupta
Shubham Gupta

Reputation: 1

There are two ways to tackle this. Either you can convert all variables to string and use replace function. Or you can try to pick up all float values using a mask and then replace it with value P. This would be much more easier to identify.

Method 1

df.applymask(str).replace("^\s*[-+]?[0-9]*\.?[0-9]*\s*$",'P')

Method 2

def find_float(x):
    try:
        float(x)
        return True
    except:
        return False

# Had to fill NA values with 'None' because default NA, numpy.NaN is also a float value.

mask = df.fillna('None').applymap(find_float)

df.where(mask,'P')

Upvotes: 0

wwnde
wwnde

Reputation: 26676

if the digits are already string;

df.replace(regex=r'\d\.\d+', value='P')

If the digits are dtype float;

df.astype(str).replace(regex=r'\d\.\d+', value='P')

     Names  01/04/2020 02/04/2020
0  Name1          P           nan
1  Name2          P             P

Upvotes: 1

RavinderSingh13
RavinderSingh13

Reputation: 133670

Could you please try following, written on mobile so couldn't test it yet, should work though I believe.

import pandas as pd
df.replace(regex=r'([0-9]{1,})?\.[0-9]{1,}$', value='P')

Upvotes: 3

BENY
BENY

Reputation: 323326

Try mask with to_numeric

df = df.mask(df.apply(lambda x : pd.to_numeric(x,errors='coerce')).notnull(),'P')
df
Out[19]: 
   Names 01/04/2020 02/04/2020...
0  Name1          P           NaN
1  Name2          P             P

Upvotes: 3

Related Questions