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

xiaoxiao2021-02-28  67

最近做一个系列博客,跟着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 pd from numpy.random import randint df = 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 time import pandas as pd from numpy.random import randint # 采用 loc t = 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) # 采用 append t = 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的错误。

转载请注明原文地址: https://www.6miu.com/read-35843.html

最新回复(0)