Appending a list or series to a pandas DataFrame as a row ?

Appending a list or series to a pandas DataFrame as a row ?

Asked on December 24, 2018 in Pandas.
Add Comment


  • 2 Answer(s)

    The best method is to append before creating the dataframe.

    >>> import pandas as pd
    >>> list=[['a','b']]
    >>> list.append(['e','f'])
    >>> df=pd.DataFrame(list,columns=['col1','col2'])
      col1 col2
    0  a    b
    1  e    f
    
    Answered on December 24, 2018.
    Add Comment

    The best solution:

    >>> import pandas as pd
    >>> df = pd.DataFrame(columns=['col1', 'col2'])
    >>> df = df.append(pd.Series(['a', 'b'], index=['col1','col2']), ignore_index=True)
    >>> df = df.append(pd.Series(['d', 'e'], index=['col1','col2']), ignore_index=True)
    >>> df
      col1 col2
    0  a    b
    1  d    e
    
    Answered on December 24, 2018.
    Add Comment


  • Your Answer

    By posting your answer, you agree to the privacy policy and terms of service.