![]() |
VOOZH | about |
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
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:
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:
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: