Python Practice › Pandas Programs › Program 40
Add a row
Write a pandas program to add a new row.
Output
name age 0 Alex 30 1 John 27 2 Peter 29 3 Klaus 33 4 Jason 28
Approach
df.loc[len(df)] = [...]orpd.concat.
Solution
import pandas as pddf = pd.DataFrame({"name": ["Alex", "John", "Peter", "Klaus"], "age": [30, 27, 29, 33]})df.loc[len(df)] = ["Jason", 28]print(df)Output
name age 0 Alex 30 1 John 27 2 Peter 29 3 Klaus 33 4 Jason 28
Learn the concept
New to this topic? Read the lesson, then come back.