Python Practice › Pandas Programs › Program 46
Add a column
Write a pandas program to add a salary column.
Output
name age salary 0 Alex 30 50000 1 John 27 65000 2 Peter 29 58000 3 Klaus 33 66000
Approach
- Assign a list to a new column name.
Solution
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})df["salary"] = [50000, 65000, 58000, 66000]print(df)Output
name age salary 0 Alex 30 50000 1 John 27 65000 2 Peter 29 58000 3 Klaus 33 66000
Learn the concept
New to this topic? Read the lesson, then come back.