5

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?

2
  • 1
    df.append(df.iloc[0])? Commented Nov 26, 2017 at 20:09
  • df.loc[len(df)] = df.iloc[0] Commented Nov 26, 2017 at 20:11

2 Answers 2

8

You can try:

df = df.append(df.iloc[0], ignore_index=True)
Sign up to request clarification or add additional context in comments.

Comments

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)

Comments

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.