【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行

来源:互联网 发布:linux命令执行过程 编辑:程序博客网 时间:2024/05/20 13:13

最近做一个系列博客,跟着stackoverflow学Pandas。

专栏地址:http://blog.csdn.net/column/details/16726.html

以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序:
https://stackoverflow.com/questions/tagged/pandas?sort=votes&pageSize=15

add one row in a pandas.DataFrame -DataFrame添加行

https://stackoverflow.com/questions/10715965/add-one-row-in-a-pandas-dataframe

不得不说,这个问题在stackoverflow有10个回答,303 votes,339k views但是最终没有得出一个比较好的答案。

下面例举几个可以实现的操作

loc、iloc

df = DataFrame(columns=('lib', 'qty1', 'qty2'))for i in range(5):    df.loc[i] = [randint(-1,1) for n in range(3)]    # loc可以对没有的 index 进行赋值,而 iloc 则不允许,iloc只能对已经存在的位置进行操作。print(df)#     lib  qty1  qty2# 0    0     0    -1# 1   -1    -1     1# 2    1    -1     1# 3    0     0     0# 4    1    -1    -1

这是一种方法,但是如果我们是往已有的DataFrame中添加数据,而已有的DataFrame已经存在相同的index就会造成替换。

当然如果我们对我们的数据足够了解,或者index并不重要,我们可以对index按0-based重新赋值。然后添加新行,也就不会出现问题。

append

我个人比较喜欢采用append函数,进行叠加,可以避免上面提到的相同index造成的替换问题。

可以参考:
https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.append.html

import pandas as pdfrom numpy.random import randintdf = pd.DataFrame(columns=('lib', 'qty1', 'qty2'))for i in xrange(5):    s = pd.Series({'lib':randint(-1,1), 'qty1':randint(-1,1), 'qty2':randint(-1,1)})    # 这里 Series 必须是 dict-like 类型    df = df.append(s, ignore_index=True)# 这里必须选择ignore_index=True 或者给 Series 一个index值

时间测评

import timeimport pandas as pdfrom numpy.random import randint# 采用 loct = time.time()df = pd.DataFrame(columns=('lib', 'qty1', 'qty2'))for i in xrange(10000):     df.loc[i] = [randint(-1,1) for n in range(3)]       print('loc:', time.time() - t)# 采用 appendt = time.time()df = pd.DataFrame(columns=('lib', 'qty1', 'qty2'))for i in xrange(10000):    s = pd.Series({'lib':randint(-1,1), 'qty1':randint(-1,1), 'qty2':randint(-1,1)})    df = df.append(s, ignore_index=True)print('apped:', time.time() - t)# ('loc:', 18.150289058685303)# ('apped:', 15.132553100585938)

可以看出,采用 append 的方法速度上比较快,而且可以避免index的错误。

阅读全文
1 0
原创粉丝点击