Append an empty row in dataframe using pandas

前端 未结 8 1865
忘了有多久
忘了有多久 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:15

    You can add it by appending a Series to the dataframe as follows. I am assuming by blank you mean you want to add a row containing only "Nan". You can first create a Series object with Nan. Make sure you specify the columns while defining 'Series' object in the -Index parameter. The you can append it to the DF. Hope it helps!

    from numpy import nan as Nan
    import pandas as pd
    
    >>> df1 = pd.DataFrame({'A': ['A0', 'A1', 'A2', 'A3'],
    ...                     'B': ['B0', 'B1', 'B2', 'B3'],
    ...                     'C': ['C0', 'C1', 'C2', 'C3'],
    ...                     'D': ['D0', 'D1', 'D2', 'D3']},
    ...                     index=[0, 1, 2, 3])
    
    >>> s2 = pd.Series([Nan,Nan,Nan,Nan], index=['A', 'B', 'C', 'D'])
    >>> result = df1.append(s2)
    >>> result
         A    B    C    D
    0   A0   B0   C0   D0
    1   A1   B1   C1   D1
    2   A2   B2   C2   D2
    3   A3   B3   C3   D3
    4  NaN  NaN  NaN  NaN
    
    0 讨论(0)
  • 2021-02-01 14:20

    You can also use:

    your_dataframe.insert(loc=0, value=np.nan, column="")
    

    where loc is your empty row index.

    0 讨论(0)
提交回复
热议问题