Swapnil Kumar
Swapnil Kumar

Reputation: 3

Modifying column values in Pandas?

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 |

Upvotes: -1

Views: 54

Answers (1)

Peaky_001
Peaky_001

Reputation: 1137

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

Upvotes: 1

Related Questions