最近做一个系列博客,跟着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

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

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

时间测评

  1. import time
  2. import pandas as pd
  3. from numpy.random import randint
  4. # 采用 loc
  5. t = time.time()
  6. df = pd.DataFrame(columns=('lib', 'qty1', 'qty2'))
  7. for i in xrange(10000):
  8. df.loc[i] = [randint(-1,1) for n in range(3)]
  9. print('loc:', time.time() - t)
  10. # 采用 append
  11. t = time.time()
  12. df = pd.DataFrame(columns=('lib', 'qty1', 'qty2'))
  13. for i in xrange(10000):
  14. s = pd.Series({'lib':randint(-1,1), 'qty1':randint(-1,1), 'qty2':randint(-1,1)})
  15. df = df.append(s, ignore_index=True)
  16. print('apped:', time.time() - t)
  17. # ('loc:', 18.150289058685303)
  18. # ('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. web.xml servlet、servlet-mapping配置

    Servlet常称为服务器端小程序,即运行在服务器端的程序,用于处理及响应客户的请求. Servlet类是个特殊的java类,继承于HttpServlet. --------------------- ...

  2. 20145201李子璇《网络对抗》逆向及Bof基础实践

    20145201李子璇<网络对抗>逆向及Bof基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回 ...

  3. C#开发自己的Web服务器

    介绍 我们将学习如何写一个简单的web服务器,用于响应知名的HTTP请求(GET和POST),用C#发送响应.然后,我们从网络访问这台服务器,这次我们会说“Hello world!” 背景 HTTP协 ...

  4. centos查看是否安装了某个软件

    1. rpm包安装的,可以用rpm -qa看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字". 2. yum方法安装的,可以用yum list ...

  5. consul 小結

    Consul Config 使用Git做版本控制的实现 https://segmentfault.com/a/1190000013807641 服务发现 - consul 的介绍.部署和使用 http ...

  6. Google Java编程风格指南中文版(转)

    作者:Hawstein出处:http://hawstein.com/posts/google-java-style.html声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Cre ...

  7. shell 数组【了解一下】

    数组编程 #!/bin/bash # array soft=( php mysql nginx ) # 输出第一个 echo ${soft[0]} # 输出所有 echo "This sof ...

  8. javascript 关键词 new都做了写什么

    翻译自stackoverflow:http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript ne ...

  9. 常数PK系列汇总

    常数PK系列说明: 在AC的情况下得分=\(\sum_{i=1}^{10}{1000-runtime\_on\_point_i}\) RE会显示UKE UPD:之前的数据太水,导致好多题都在9000分 ...

  10. 十六 web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS

    PhantomJS虚拟浏览器 phantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,利用这个软件,可以获取到网址js加载的任何信息,也就是可以获取浏览器异步加载的 ...