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

后端 未结 12 1996
一向
一向 2020-12-02 05:22

So I have initialized an empty pandas DataFrame and I would like to iteratively append lists (or Series) as rows in this DataFrame. What is the best way of doing this?

12条回答
  •  猫巷女王i
    2020-12-02 05:38

    Could you do something like this?

    >>> 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
    

    Does anyone have a more elegant solution?

提交回复
热议问题