python中pandas.DataFrame的简单操作方法(创建、索引、增添与删除)


Posted in Python onMarch 12, 2017

前言

最近在网上搜了许多关于pandas.DataFrame的操作说明,都是一些基础的操作,但是这些操作组合起来还是比较费时间去正确操作DataFrame,花了我挺长时间去调整BUG的。我在这里做一些总结,方便你我他。感兴趣的朋友们一起来看看吧。

一、创建DataFrame的简单操作:

1、根据字典创造:

In [1]: import pandas as pd
In [3]: aa={'one':[1,2,3],'two':[2,3,4],'three':[3,4,5]}
In [4]: bb=pd.DataFrame(aa)
In [5]: bb
Out[5]: 
 one three two
0 1 3 2
1 2 4 3
2 3 5 4`

字典中的keys就是DataFrame里面的columns,但是没有index的值,所以需要自己设定,不设定默认是从零开始计数。

bb=pd.DataFrame(aa,index=['first','second','third'])
bb
Out[7]: 
 one three two
first 1 3 2
second 2 4 3
third 3 5 4

2、从多维数组中创建

import numpy as np
In [9]: del aa
In [10]: aa=np.array([[1,2,3],[4,5,6],[7,8,9]])
In [11]: aa
Out[11]: 
array([[1, 2, 3],
 [4, 5, 6],
 [7, 8, 9]])
In [12]: bb=pd.DataFrame(aa)
In [13]: bb
Out[13]: 
 0 1 2
0 1 2 3
1 4 5 6
2 7 8 9

从多维数组中创建就需要为DataFrame赋值columns和index,否则就是默认的,很丑的。

bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three'])
In [15]: bb
Out[15]: 
 one two three
22 1 2 3
33 4 5 6
44 7 8 9

3、用其他的DataFrame创建

bb=pd.DataFrame(aa,index=[22,33,44],columns=['one','two','three'])
bb
Out[15]: 
 one two three
22 1 2 3
33 4 5 6
44 7 8 9
cc=bb[['one','three']].copy()
Cc
Out[17]: 
 one three
22 1 3
33 4 6
44 7 9

这里的拷贝是深拷贝,改变cc中的值并不能改变bb中的值。

cc['three'][22]=5
bb
Out[19]: 
 one two three
22 1 2 3
33 4 5 6
44 7 8 9

cc
Out[20]: 
 one three
22 1 5
33 4 6
44 7 9

二、DataFrame的索引操作:

对于一个DataFrame来说,索引是最烦的,最易出错的。

1、索引一列或几列,比较简单:

bb['one']
Out[21]: 
22 1
33 4
44 7
Name: one, dtype: int32

多个列名需要将输入的列名存在一个列表里,才是个collerable的变量,否则会报错。

bb[['one','three']]
Out[29]: 
 one three
22 1 3
33 4 6
44 7 9

2、索引一条记录或几条记录:

bb[1:3]
Out[27]: 
 one two three
33 4 5 6
44 7 8 9
bb[:1]
Out[28]: 
 one two three
22 1 2 3

这里注意冒号是必须有的,否则是索引列的了。

3、索引某几列的变量的某几条记录,这个折磨了我好久:

第一种

bb.loc[[22,33]][['one','three']]
Out[30]: 
 one three
22 1 3
33 4 6

这种不能改变这里面的值,你只是能读值,不能写值,可能和loc()函数有关:

bb.loc[[22,33]][['one','three']]=[[2,2],[3,6]]
In [32]: bb
Out[32]: 
 one two three
22 1 2 3
33 4 5 6
44 7 8 9

第二种:也是只能看

bb[['one','three']][:2]
Out[33]: 
 one three
22 1 3
33 4 6

想要改变其中的值就会报错。

In [34]: bb[['one','three']][:2]=[[2,2],[2,2]]
-c:1: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame.
Try using .loc[row_index,col_indexer] = value instead
F:\Anaconda\lib\site-packages\pandas\core\frame.py:1999: SettingWithCopyWarning: A value is trying to be set on a copy of a slice from a DataFrame
 return self._setitem_slice(indexer, value)

第三种:可以改变数据的值!!!

Iloc是按照数据的行列数来索引,不算index和columns

bb.iloc[2:3,2:3]
Out[36]: 
 three
44 9

bb.iloc[1:3,1:3]
Out[37]: 
 two three
33 5 6
44 8 9
bb.iloc[0,0]
Out[38]: 1

下面是证明:

bb.iloc[0:4,0:2]=[[9,9],[9,9],[9,9]]
In [45]: bb
Out[45]: 
 one two three
22 9 9 3
33 9 9 6
44 9 9 9

三、在原有的DataFrame上新建一个columns或几个columns

1、什么都不用的,只能单独创建一列,多列并不好使,亲测无效:

bb['new']=[2,3,4]
bb
Out[51]: 
 one two three new
22 9 9 3 2
33 9 9 6 3
44 9 9 9 4
bb[['new','new2']]=[[2,3,4],[5,3,7]]
KeyError: "['new' 'new2'] not in index"

赋予的list基本就是按照所给index值顺序赋值,可是一般我们是要对应的index进行赋值,想要更高级的赋值就看后面的了。

2、使用字典进行多列按index赋值:

aa={33:[234,44,55],44:[657,77,77],22:[33,55,457]}
In [58]: bb=bb.join(pd.DataFrame(aa.values(),columns=['hi','hello','ok'],index=aa.keys()))
In [59]: bb
Out[59]: 
 one two three new hi hello ok
22 9 9 3 2 33 55 457
33 9 9 6 3 234 44 55
44 9 9 9 4 657 77 77

这里aa是一个字典和列表的嵌套,相当于一条记录,使用keys当做index名而不是一般默认的columns名。达到了按index多列匹配的目的。由于dict()储存是混乱的,之间用dict()而不给他的index赋值会记录错乱,这一点注意值得注意。

四、删除多列或多记录:

删除列

bb.drop(['new','hi'],axis=1)
Out[60]: 
 one two three hello ok
22 9 9 3 55 457
33 9 9 6 44 55
44 9 9 9 77 77

删除记录

bb.drop([22,33],axis=0)
Out[61]: 
 one two three new hi hello ok
44 9 9 9 4 657 77 77

跟大家分享一篇关于python中pandas.DataFrame对行与列求和及添加新行与列示例,感兴趣的朋友们可以看看。

DataFrame还有很多功能还没有涉及,等以后有涉及到,看完官网的API之后,还会继续分享,everything is ok。

总结

以上就是这篇文章的全部内容了,希望本文的内容对大家的学习或者工作能带来一定的帮助,如果有疑问大家可以留言交流,谢谢大家对三水点靠木的支持。

Python 相关文章推荐
Python抓取百度查询结果的方法
Jul 08 Python
使用Python编写简单的画图板程序的示例教程
Dec 08 Python
Python实现可设置持续运行时间、线程数及时间间隔的多线程异步post请求功能
Jan 11 Python
Python实现去除列表中重复元素的方法小结【4种方法】
Apr 27 Python
对python的bytes类型数据split分割切片方法
Dec 04 Python
python使用suds调用webservice接口的方法
Jan 03 Python
python实现烟花小程序
Jan 30 Python
python 用for循环实现1~n求和的实例
Feb 01 Python
pytorch 实现查看网络中的参数
Jan 06 Python
keras 指定程序在某块卡上训练实例
Jun 22 Python
Python如何实现自带HTTP文件传输服务
Jul 08 Python
深入了解Python装饰器的高级用法
Aug 13 Python
Python中str.format()详解
Mar 12 #Python
python中pandas.DataFrame对行与列求和及添加新行与列示例
Mar 12 #Python
Python中关键字nonlocal和global的声明与解析
Mar 12 #Python
Python中模块string.py详解
Mar 12 #Python
Python中第三方库Requests库的高级用法详解
Mar 12 #Python
python 获取网页编码方式实现代码
Mar 11 #Python
python 爬虫出现403禁止访问错误详解
Mar 11 #Python
You might like
BBS(php & mysql)完整版(七)
2006/10/09 PHP
支持数组的ADDSLASHES的php函数
2010/02/16 PHP
PHP易混淆函数的区别及用法汇总
2014/11/22 PHP
Symfony2实现在controller中获取url的方法
2016/03/18 PHP
php 判断字符串编码是utf-8 或gb2312实例
2016/11/01 PHP
PHP小程序支付功能完整版【基于thinkPHP】
2019/03/26 PHP
javascript form 验证函数 弹出对话框形式
2009/06/23 Javascript
jquery简单实现滚动条下拉DIV固定在头部不动
2013/11/25 Javascript
node.js中的console.warn方法使用说明
2014/12/09 Javascript
vue货币过滤器的实现方法
2017/04/01 Javascript
js实现图片懒加载效果
2017/07/17 Javascript
jQuery Datatable 多个查询条件自定义提交事件(推荐)
2017/08/24 jQuery
jQuery中extend函数简单用法示例
2017/10/11 jQuery
深入理解Vue官方文档梳理之全局API
2017/11/22 Javascript
Vue2.0实现调用摄像头进行拍照功能 exif.js实现图片上传功能
2018/04/28 Javascript
Vue中 key keep-alive的实现原理
2018/09/18 Javascript
微信小程序的注册页面包含倒计时验证码、获取用户信息
2019/05/22 Javascript
[07:57]DOTA2热力大趴狂欢夜 广州站活动回顾
2013/11/27 DOTA
[44:22]完美世界DOTA2联赛循环赛 FTD vs PXG BO2第一场 11.01
2020/11/02 DOTA
[00:57]英雄,你的补给到了!
2020/11/13 DOTA
Python处理RSS、ATOM模块FEEDPARSER介绍
2015/02/18 Python
Python求算数平方根和约数的方法汇总
2016/03/09 Python
Python使用cx_Oracle模块操作Oracle数据库详解
2018/05/07 Python
python实现雨滴下落到地面效果
2018/06/21 Python
在Pycharm中修改文件默认打开方式的方法
2019/01/17 Python
python tkinter之顶层菜单、弹出菜单实例
2020/03/04 Python
HTML5实现签到 功能
2018/10/09 HTML / CSS
英国领先的大码时装品牌之一:Elvi
2018/08/26 全球购物
MADE法国:提供原创设计师家具
2018/09/18 全球购物
校园报刊亭创业计划书
2014/01/02 职场文书
致全体运动员广播稿
2014/02/01 职场文书
学习十八大的心得体会
2014/09/01 职场文书
研修心得体会
2014/09/04 职场文书
初中作文评语集锦
2014/12/25 职场文书
社区党务工作总结2015
2015/05/19 职场文书
2016年学校爱国卫生月活动总结
2016/04/06 职场文书