Reputation: 573
I want to change the data type of column value based on row condition, but not working
Sample Data:
eName Value1 Value2 NValue
sample1 XYZ XYZ XYZ
sample2 102 XYZ 102 XYZ 102 XYZ
sample3 3 3.000
sample4 3 3.000
I have tried this approach, Is there different approach I have to use.
data.loc[data.eName == 'sample3', ['Value1', 'Value2']].apply(pd.to_numeric)
data.loc[data.eName == 'sample4', ['Value1', 'Value2']].apply(pd.to_numeric)
Output as:
eName Value1 Value2 NValue
sample1 XYZ XYZ XYZ
sample2 102 XYZ 102 XYZ 102 XYZ
sample3 3.00 3.00
sample4 3.00 3.00
Upvotes: 1
Views: 307
Reputation: 862581
You can assign mask created by Series.isin
to both sides of filtered DataFrame and if necessary add errors='coerce'
for convert non numeric values to NaN
:
m = data.eName.isin(['sample3','sample4'])
cols = ['Value1', 'Value2']
#if need all columns without eName
#cols = df.columns.difference(['eName'])
data.loc[m, cols] = data.loc[m , cols].apply(pd.to_numeric, errors='coerce')
print (data)
eName Value1 Value2 NValue
0 sample1 XYZ XYZ XYZ
1 sample2 102 XYZ 102 XYZ 102 XYZ
2 sample3 3 3 NaN
3 sample4 3 3 NaN
If need all columns without first:
m = data.eName.isin(['sample3','sample4']).values
data.iloc[m, 1:] = data.iloc[m , 1:].apply(pd.to_numeric, errors='coerce')
Upvotes: 3