0 1 2
0 1 2 3
1 4 5 6
2 10 11 12
3 7 8 9
As we can see the row containing [10, 11, 12] got inserted after the row with index 1. We can use the following Python code to do the same:
import pandas
list1 = [[1, 2, 3], [4, 5, 6], [7, 8, 9]]
df = pandas.DataFrame(list1)
print("df before adding row: \n", df)
df.loc[1.5] = [10, 11, 12]
df = df.sort_index().reset_index(drop=True)
print("df after adding row: \n", df)
Here, we want to add a row after row index 1. So, we are first adding the row using df.loc[1.5]. After that, we are sorting the row indexes and resetting the indexes of the DataFrame. Please note that drop=True parameter ensures that the old indexes are not added to the DataFrame as a column.
The output of the above program will be:
df before adding row:
0 1 2
0 1 2 3
1 4 5 6
2 7 8 9
df after adding row:
0 1 2
0 1 2 3
1 4 5 6
2 10 11 12
3 7 8 9








































0 Comments