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

后端 未结 12 1997
一向
一向 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条回答
  •  醉酒成梦
    2020-12-02 05:37

    Sometimes it's easier to do all the appending outside of pandas, then, just create the DataFrame in one shot.

    >>> import pandas as pd
    >>> simple_list=[['a','b']]
    >>> simple_list.append(['e','f'])
    >>> df=pd.DataFrame(simple_list,columns=['col1','col2'])
       col1 col2
    0    a    b
    1    e    f
    

提交回复
热议问题