VOOZH about

URL: https://www.geeksforgeeks.org/pandas/insert-row-at-given-position-in-pandas-dataframe/

⇱ Insert row at given position in Pandas Dataframe - GeeksforGeeks


  • Courses
  • Tutorials
  • Interview Prep

Insert row at given position in Pandas Dataframe

Last Updated : 3 Oct, 2025

If a DataFrame has existing rows, you can insert a new row at any position without disturbing the other rows. For Example, suppose the DataFrame has [A, B, C] and you insert X at position 1, updated DataFrame becomes [A, X, B, C]

Let’s consider this DataFrame which we will use in all examples:

Output

Date Event Cost
0 10/2/2011 Music 10000
1 12/2/2011 Poetry 5000
2 13/2/2011 Theatre 15000
3 14/2/2011 Comedy 2000

Examples

Example 1: In this example, we insert a row at position 2 using simple slicing and concat.

Output

Date Event Cost
0 10/2/2011 Music 10000
1 12/2/2011 Poetry 5000
2 11/2/2011 Wrestling 12000
3 13/2/2011 Theatre 15000
4 14/2/2011 Comedy 2000

Explanation:

  • upper = df[:2]: slice DataFrame to get rows before position 2.
  • lower = df[2:]: slice DataFrame to get rows from position 2 onward.
  • pd.concat([...]): combine upper, new row (converted to DataFrame), and lower.
  • .reset_index(drop=True): reset index to maintain consecutive integers.

Example 2: In this example, we insert a row at position 1 directly using loc.

Output

Date Event Cost
0 10/2/2011 Music 10000
1 12/2/2011 Poetry 5000
2 11/2/2011 Gymnastics 11000
3 13/2/2011 Theatre 15000
4 14/2/2011 Comedy 2000

Explanation:

  • df2 = df.copy(): create a copy of the original DataFrame to avoid changes.
  • df2.loc[1.5] = new_row2: insert row at a temporary fractional index.
  • df2.sort_index().reset_index(drop=True): sort by index and reset to maintain integer order.

Example 3: In this example, we insert a new row at the end of the DataFrame using loc.

Output

Date Event Cost
0 10/2/2011 Music 10000
1 12/2/2011 Poetry 5000
2 13/2/2011 Theatre 15000
3 14/2/2011 Comedy 2000
4 15/2/2011 Dance 9000

Explanation:

  • df3 = df.copy(): copy original DataFrame.
  • df3.loc[df3.shape[0]] = row3: insert row at the next available index (end).
Comment

Explore