Search code snippets, questions, articles...

[Pandas] Add new column to DataFrame based on existing column

If you want to add a new column to Pandas Dataframe that will have the values based on existing column values then you can use the methods explained in this post.
import pandas as pd

employees = {
  "name": ["Richard", "Patrick", "Sam", "Vick"],
  "address": ["New York", "Atlanta", "Canada", "India"],
  "salary": [10000, 20000, 15000, 22000]
}

# create dataframe
df = pd.DataFrame(employees)

print(df)

# add new column - deduction that has 10% of salary
df["deduction"] = df["salary"] * 10 / 100

print(df)
Best JSON Validator, JSON Tree Viewer, JSON Beautifier at same place. Check how cool is the tool

Output

      name   address  salary
0  Richard  New York   10000
1  Patrick   Atlanta   20000
2      Sam    Canada   15000
3     Vick     India   22000


      name   address  salary  deduction
0  Richard  New York   10000     1000.0
1  Patrick   Atlanta   20000     2000.0
2      Sam    Canada   15000     1500.0
3     Vick     India   22000     2200.0

Here we are adding a new column deduction that will be based on the values of the salary column in Dataframe df.

Use apply() function to add a column based on another column

import pandas as pd

employees = {
  "name": ["Richard", "Patrick", "Sam", "Vick"],
  "address": ["New York", "Atlanta", "Canada", "India"],
  "salary": [10000, 20000, 15000, 22000]
}

# create dataframe
df = pd.DataFrame(employees)

# add new column - deduction that has 10% of salary
df["deduction"] = df.apply(lambda row : row['salary'] * 10 / 100, axis=1)

print(df)

Output

      name   address  salary  deduction
0  Richard  New York   10000     1000.0
1  Patrick   Atlanta   20000     2000.0
2      Sam    Canada   15000     1500.0
3     Vick     India   22000     2200.0
Was this helpful?
0 Comments