0

I want to convert all values < 100 to 0 in column ODOMETER_FW. I have below DF:

enter image description here

When I use pandas:

stupid_values = fuel_GB['ODOMETER_FW'].replace(fuel_GB['ODOMETER_FW']<100,0)
fuel_GB['ODOMETER_FW'] = stupid_values
fuel_GB.head(13)

And the result as you can see, has some error and I really do not know why.

enter image description here

Ventre90
  • 57
  • 6

3 Answers3

1

Just ask the modification for the relevant lines:

fuel_GB.loc[fuel_GB['ODOMETER_FW'] < 100, 'ODOMETER_FW'] = 0
Serge Ballesta
  • 143,923
  • 11
  • 122
  • 252
1

Use lambda function to convert values less than 100 to 0:

df['ODOMETER_FW'] = df['ODOMETER_FW'].apply(lambda x: 0 if x <100 else x)
print(df)
   ODOMETER_FW
0         11833
1             0
2          9080
3          8878
4             0
5         14578
6         14351
7             0
8         13456
9             0
10            0
11            0
12            0
ManojK
  • 1,570
  • 2
  • 9
  • 17
  • Why use `apply()` and a lambda instead of simply `fuel_GB[fuel_GB['ODOMETER_FW'] < 100, 'ODOMETER_FW'] = 0` ? – AMC Mar 16 '20 at 20:57
0

Use this pandas code:

fuel_GB[fuel_GB['ODOMETER_FW'] < 100] = 0
carnava1
  • 319
  • 2
  • 6
  • Hi it is changing all values in mentioned rows on 0. I need 0 only in ODOMETER_FW column when start value is < 100. But thank you for help. – Ventre90 Mar 17 '20 at 09:02