Converting a column into floats except first few rows i.e selective conversion in a column

150 views Asked by At

I m trying to do some data processing but I m getting the same error everytime. My dataframe (con_tc) looks like this as follows:-

Index       u_p0       u_p1      u_p2.........u_p100
x            0          0          0            0
y            0          0          0            0
z            30         50         75          1000 
0.01        0.5        0.6       0.43          0.83
0.02        0.56       0.94      0.94          0.7
....
1000        0.4        0.5       0.45          0.56

When I run this line of code

con_tc.index = con_tc.index.map(lambda w: float(w) if (w not in 'xyz') else w)

which is trying to clean the index into float, I am getting the error as

TypeError: 'in <string>' requires string as left operand, not float

The aim behind this is to convert all the numeric values into floats except x,y and z. In basic term

Index
  x
  y
  z
  0.01
  0.02
 ....
  1000

If anyone can help me out it will be really helpful.

1

There are 1 answers

0
abak1802 On

May be you can use this way

con_tc[ 'float_u_p_0' ] = con_tc.apply( lambda x: float(x.u_p0) if x.Index  not in ("xyz") else x.u_p0