1

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.

Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
  • can you explain why you want to convert Index to float? Shouldn't you leave that for panda to take care. Also, do you want to convert the data in the columns to float or the column itself to float? – Joe Ferndz Jan 21 '21 at 04:31

1 Answers1

0

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
Joe Ferndz
  • 8,417
  • 2
  • 13
  • 33
abak1802
  • 109
  • 1
  • 7