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

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

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

【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行的更多相关文章

  1. 【跟着stackoverflow学Pandas】How to iterate over rows in a DataFrame in Pandas-DataFrame按行迭代

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  2. 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  3. 【跟着stackoverflow学Pandas】 -Get list from pandas DataFrame column headers - Pandas 获取列名

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  4. 【跟着stackoverflow学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  5. 【跟着stackoverflow学Pandas】“Large data” work flows using pandas-pandas大数据处理流程

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  6. 【跟着stackoverflow学Pandas】Delete column from pandas DataFrame-删除列

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  7. 【跟着stackoverflow学Pandas】Renaming columns in pandas-列的重命名

    最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...

  8. [LeetCode] Add One Row to Tree 二叉树中增加一行

    Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...

  9. [Swift]LeetCode623. 在二叉树中增加一行 | Add One Row to Tree

    Given the root of a binary tree, then value v and depth d, you need to add a row of nodes with value ...

随机推荐

  1. 20145302张薇《Java程序设计》第四周学习总结

    20145302 <Java程序设计>第四周学习总结 教材学习内容总结 第六章 继承共同行为 面向对象中,子类继承父类,避免重复的行为定义,不过并非为了避免重复定义行为就使用继承. 程序代 ...

  2. 20145324Java课程总结

    20145324课程总结 ●每周读书笔记链接总汇 [第一周](http://www.cnblogs.com/SJZGM10/p/5246770.html) 第二周(不小心删了) [第三周](http: ...

  3. sqoop将mysql的tinyint类型转化为boolean类型

    当mysql中的字段为tinyint类型时,通过sqoop导入数据到hdfs上,在hdfs上显示的是true,false值.为了显示0,1. 解决方法: jdbc:mysql://<hostna ...

  4. openwrt下如何只编译uboot

    答:如使用以下命令编译nxp的layerscape系列芯片相关的uboot: make package/boot/uboot-layerscape/prepare make package/boot/ ...

  5. [BZOJ4016]最短路径树问题

    Description 给一个包含n个点,m条边的无向连通图.从顶点1出发,往其余所有点分别走一次并返回. 往某一个点走时,选择总长度最短的路径走.若有多条长度最短的路径,则选择经过的顶点序列字典序最 ...

  6. PHP设计模式(五): 原型模式

  7. base64 原理

    Base64编码之所以称为Base64,是因为其使用64个字符来对任意数据进行编码,同理有Base32.Base16编码.标准Base64编码使用的64个字符为: 这64个字符是各种字符编码(比如AS ...

  8. 在centos7上安装部署hadoop2.7.3和spark2.0.0

    一.安装装备 下载安装包: vmware workstations pro 12 三台centos7.1 mini 虚拟机 网络配置NAT网络如下: 二.创建hadoop用户和hadoop用户组 1. ...

  9. python的变量,对象的内存地址以及参数传递过程

    作为一个由c/c++转过来的菜鸟,刚接触Python的变量的时候很不适应,应为他的行为很像指针,void* ,不知道大家有没有这样的感觉.其实Python是以数据为本,变量可以理解为标签.作为c/c+ ...

  10. Python基础笔记系列七:字符串定义和访问

    本系列教程供个人学习笔记使用,如果您要浏览可能需要其它编程语言基础(如C语言),why?因为我写得烂啊,只有我自己看得懂!! 字符串定义和访问 1.字符串基础 a.字符串可以用单引号.双引号.三引号( ...