Add empty row with index in a Pandas dataframe

Question:

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?

Asked By: Joe

||

Answers:

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'] })
Answered By: BENY

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']))])
Answered By: HandyManDan
Categories: questions Tags: , , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.