1

i have a dataframe

id    main_value
1      10
2      3
4      1
6      10

i want to change main_value of id = 4,such that it should decrement by 2.

i know a method using .loc

freq = 3
if freq == 3:
    df.loc[df.id==4, ['main_value']] = df.main_value.loc[df.id==4] - 2

But this seems very lengthy, is there a better way to do this?

1 Answer 1

2

I think you can use:

df.loc[df.id==4, 'main_value'] -= 2 
print (df)

   id  main_value
0   1          10
1   2           3
2   4          -1
3   6          10
Sign up to request clarification or add additional context in comments.

1 Comment

Yes, this shorcuts is nice, but easy forgetable ;)

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.