Append an empty row in dataframe using pandas

前端 未结 8 1983
忘了有多久
忘了有多久 2021-02-01 13:25

I am trying to append an empty row at the end of dataframe but unable to do so, even trying to understand how pandas work with append function and still not getting it.

8条回答
  •  半阙折子戏
    2021-02-01 14:01

    Assuming df is your dataframe,

    df_prime = pd.concat([df, pd.DataFrame([[np.nan] * df.shape[1]], columns=df.columns)], ignore_index=True)
    

    where df_prime equals df with an additional last row of NaN's.

    Note that pd.concat is slow so if you need this functionality in a loop, it's best to avoid using it. In that case, assuming your index is incremental, you can use

    df.loc[df.iloc[-1].name + 1,:] = np.nan
    

提交回复
热议问题