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)
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.
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
0 Comments