-1

How do I modify a coumn value in Pandas where new values are two times the previous values?

I tried this:

employees.loc['salary'] = employees['salary']*2

Input: DataFrame employees

+---------+--------+
| name    | salary |
+---------+--------+
| Jack    | 19666  |
| Piper   | 74754  |
| Mia     | 62509  |
| Ulysses | 54866  |

Output:

| name    | salary |
+---------+--------+
| Jack    | 39332  |
| Piper   | 149508 |
| Mia     | 125018 |
| Ulysses | 109732 |
3
  • remove the .loc and it will work. employees['salary'] = employees['salary']*2 Commented Aug 8, 2024 at 8:05
  • This question is similar to: Modify column data based on other column in pandas. If you believe it’s different, please edit the question, make it clear how it’s different and/or how the answers on that question are not helpful for your problem. Commented Aug 8, 2024 at 8:06
  • employees.loc['salary'] will create a new row with the index salary. You could use employees.loc[:, 'salary'] = ... but there is no point as already commented - just remove the .loc Commented Aug 8, 2024 at 9:10

1 Answer 1

1

as @Emi OB said you just need to modify the 'salary' column by assigning it to itself multiplied by 2.

employees['salary'] = employees['salary'] * 2

or You can use the apply method to apply a lambda function to each element in the salary column.

employees['salary'] = employees['salary'].apply(lambda x: x * 2)

with both ways you get this output :

      name  salary
0     Jack   39332
1    Piper  149508
2      Mia  125018
3  Ulysses  109732
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.