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

1
  • Have you looked at the documentation for the method? Commented Mar 16, 2020 at 20:56

3 Answers 3

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
Sign up to request clarification or add additional context in comments.

1 Comment

Why use apply() and a lambda instead of simply fuel_GB[fuel_GB['ODOMETER_FW'] < 100, 'ODOMETER_FW'] = 0 ?
1

Just ask the modification for the relevant lines:

fuel_GB.loc[fuel_GB['ODOMETER_FW'] < 100, 'ODOMETER_FW'] = 0

3 Comments

Hi, Unfortunatelly it is not working: TypeError: 'Series' objects are mutable, thus they cannot be hashed
@Ventre90: Shame on me, I forgot the .loc. Fixed...
@ Serge Ballesta, It also works great. Thank You for your dedication!
0

Use this pandas code:

fuel_GB[fuel_GB['ODOMETER_FW'] < 100] = 0

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.