将列表或系列作为一行附加到 Pandas DataFrame? [英] Appending a list or series to a pandas DataFrame as a row?

查看:34
本文介绍了将列表或系列作为一行附加到 Pandas DataFrame?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

所以我已经初始化了一个空的 Pandas DataFrame,我想在这个 DataFrame 中迭代地附加列表(或系列)作为行.这样做的最佳方法是什么?

解决方案

有时在 Pandas 之外完成所有附加操作更容易,然后,只需一次创建 DataFrame.

<预><代码>>>>将熊猫导入为 pd>>>simple_list=[['a','b']]>>>simple_list.append(['e','f'])>>>df=pd.DataFrame(simple_list,columns=['col1','col2'])列 1 列 20 a b1 ef

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?

解决方案

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

这篇关于将列表或系列作为一行附加到 Pandas DataFrame?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆