4

I have a pandas data frame that looks something like this:

  A B C
0 1 2 3
1 4 5 6
2 7 8 9

And I would like to add row 0 to the end of the data frame and to get a new data frame that looks like this:

  A B C
0 1 2 3
1 4 5 6
2 7 8 9
3 1 2 3

What can I do in pandas to do this?

Bobby Stiller
  • 137
  • 1
  • 1
  • 9

2 Answers2

8

You can try:

df = df.append(df.iloc[0], ignore_index=True)
niraj
  • 17,498
  • 4
  • 33
  • 48
2

If you are inserting data from a list, this might help -

import pandas as pd

df = pd.DataFrame( [ [1,2,3], [2,5,7], [7,8,9]], columns=['A', 'B', 'C'])

print(df)
df.loc[-1] = [1,2,3] # list you want to insert
df.index = df.index + 1  # shifting index
df = df.sort_index()  # sorting by index
print(df)
Vivek Kalyanarangan
  • 8,951
  • 1
  • 23
  • 42