3

In all the examples and answers on here that I've seen, if there is the need to add an empty row ina Pandas dataframe, all use:

ignore_index=True

What should I do if i want to leave the current index, and append an empty row to the dataframe with a given index?

1
  • Use df1 = pd.DataFrame(np.insert(df1.values, index, values=[" "] * len(df1.columns), axis=0),columns = df1.columns) Commented Feb 1, 2019 at 7:31

3 Answers 3

7

We using reindex

df.reindex(df.index.values.tolist()+['Yourindex'])
Out[1479]: 
             A    B
0          one   Aa
1          one   Bb
2          two   Cc
Yourindex  NaN  NaN

Data input

df = pd.DataFrame({'A' : ['one', 'one', 'two'] ,
                   'B' : ['Aa', 'Bb', 'Cc'] })
Sign up to request clarification or add additional context in comments.

Comments

1
df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.loc[len(df)] = pd.Series()

or use this with insert values

df = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6]})
df.loc['Yourindex','A'] = 20

Comments

0

I use this approach...

df = pd.DataFrame({'A' : ['one', 'one', 'two'] ,
                   'B' : ['Aa', 'Bb', 'Cc'] })
new_df = pd.concat([df, pd.DataFrame(index=pd.Index(['New Index']))])

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.