Search code examples
pythonpandasindexingrow

Add empty row with index in a Pandas dataframe


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?


Solution

  • 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'] })