【跟着stackoverflow学Pandas】add one row in a pandas.DataFrame -DataFrame添加行
最近做一个系列博客,跟着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添加行的更多相关文章
- 【跟着stackoverflow学Pandas】How to iterate over rows in a DataFrame in Pandas-DataFrame按行迭代
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】 - Adding new column to existing DataFrame in Python pandas - Pandas 添加列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】 -Get list from pandas DataFrame column headers - Pandas 获取列名
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Select rows from a DataFrame based on values in a column -pandas 筛选
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】“Large data” work flows using pandas-pandas大数据处理流程
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Delete column from pandas DataFrame-删除列
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- 【跟着stackoverflow学Pandas】Renaming columns in pandas-列的重命名
最近做一个系列博客,跟着stackoverflow学Pandas. 以 pandas作为关键词,在stackoverflow中进行搜索,随后安照 votes 数目进行排序: https://stack ...
- [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 ...
- [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 ...
随机推荐
- web.xml servlet、servlet-mapping配置
Servlet常称为服务器端小程序,即运行在服务器端的程序,用于处理及响应客户的请求. Servlet类是个特殊的java类,继承于HttpServlet. --------------------- ...
- 20145201李子璇《网络对抗》逆向及Bof基础实践
20145201李子璇<网络对抗>逆向及Bof基础实践 实践目标 本次实践的对象是一个名为pwn1的linux可执行文件. 该程序正常执行流程是:main调用foo函数,foo函数会简单回 ...
- C#开发自己的Web服务器
介绍 我们将学习如何写一个简单的web服务器,用于响应知名的HTTP请求(GET和POST),用C#发送响应.然后,我们从网络访问这台服务器,这次我们会说“Hello world!” 背景 HTTP协 ...
- centos查看是否安装了某个软件
1. rpm包安装的,可以用rpm -qa看到,如果要查找某软件包是否安装,用 rpm -qa | grep "软件或者包的名字". 2. yum方法安装的,可以用yum list ...
- consul 小結
Consul Config 使用Git做版本控制的实现 https://segmentfault.com/a/1190000013807641 服务发现 - consul 的介绍.部署和使用 http ...
- Google Java编程风格指南中文版(转)
作者:Hawstein出处:http://hawstein.com/posts/google-java-style.html声明:本文采用以下协议进行授权: 自由转载-非商用-非衍生-保持署名|Cre ...
- shell 数组【了解一下】
数组编程 #!/bin/bash # array soft=( php mysql nginx ) # 输出第一个 echo ${soft[0]} # 输出所有 echo "This sof ...
- javascript 关键词 new都做了写什么
翻译自stackoverflow:http://stackoverflow.com/questions/1646698/what-is-the-new-keyword-in-javascript ne ...
- 常数PK系列汇总
常数PK系列说明: 在AC的情况下得分=\(\sum_{i=1}^{10}{1000-runtime\_on\_point_i}\) RE会显示UKE UPD:之前的数据太水,导致好多题都在9000分 ...
- 十六 web爬虫讲解2—PhantomJS虚拟浏览器+selenium模块操作PhantomJS
PhantomJS虚拟浏览器 phantomjs 是一个基于js的webkit内核无头浏览器 也就是没有显示界面的浏览器,利用这个软件,可以获取到网址js加载的任何信息,也就是可以获取浏览器异步加载的 ...