Reputation: 191
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
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.
df.applymask(str).replace("^\s*[-+]?[0-9]*\.?[0-9]*\s*$",'P')
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
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
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
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