python数据分析基础——pandas Tutorial
参考pandas官方文档:
http://pandas.pydata.org/pandas-docs/stable/10min.html#min
1.pandas中的数据类型
Series 带有索引标记的一维数组,可以存储任何数据类型
#基本方法
>>s =pd.Series(data, index=index) >>import pandas as pd
>>import numpy as np # 使用ndarray创建
>>indexs = ['a', 'b', 'c']
>>s = pd.Series(np.random.randn(3), index=indexs)
>>s
a -1.817485
b 0.012912
c 0.866929
dtype: float64
>>s.index
Index(['a', 'b', 'c'], dtype='object') #默认索引值
>>s = pd.Series(np.random.randn(3))
>>s
0 1.985833
1 0.467035
2 0.636828
dtype: float64 #使用dict创建
#默认使用dict的索引
>>d = {'a' : 0., 'b' : 1., 'c' : 2.}
>>pd.Series(d)
a 0.0
b 1.0
c 2.0
dtype: float64 #指明索引值
>>pd.Series(d, index=['b', 'c', 'd', 'a'])
b 1.0
c 2.0
d NaN
a 0.0
dtype: float64 #使用标量值创建
>>pd.Series(5., index=['a', 'b', 'c', 'd', 'e'])
a 5.0
b 5.0
c 5.0
d 5.0
e 5.0
dtype: float64
Series 类似ndarray,可以使用Numpy的很多语法
>>s = pd.Series(np.random.randn(5),index=['a', 'b', 'c', 'd', 'e'])
>>s
a -1.329486
b 0.396057
c -1.156737
d -1.152107
e -0.787661
dtype: float64 # 索引
>>s[0]
-1.3294860342555725 #切片
>>s[:3]
a -1.329486
b 0.396057
c -1.156737
dtype: float64 # 推导式
>>s[s > s.median()]
b 0.396057
e -0.787661
dtype: float64 # 按序索引
>>s[[4,3,1]]
e -0.787661
d -1.152107
b 0.396057
dtype: float64 >>np.exp(s)
a 0.264613
b 1.485954
c 0.314511
d 0.315970
e 0.454908
dtype: float64
Series 类似dict类型,可以操作索引值
>>s['a']
-1.3294860342555725 >>s['e']=12
>>s
a -1.329486
b 0.396057
c -1.156737
d -1.152107
e 12.000000
dtype: float64 >>'e' in s
True >>s.get('e')
12.0 >>s+s
a -2.658972
b 0.792115
c -2.313474
d -2.304214
e 24.000000
dtype: float64 >>s*2
a -2.658972
b 0.792115
c -2.313474
d -2.304214
e 24.000000
dtype: float64 #索引值自动对齐
#s[1:]中有a, s[:-1]中有e
>>s[1:] + s[:-1]
a NaN
b 0.792115
c -2.313474
d -2.304214
e NaN
dtype: float64
Series的name属性,创建新对象
#注意 name属性
>>s = pd.Series(np.random.randn(5),name='sth')
>>s
0 1.338578
1 2.074678
2 -0.462777
3 0.518763
4 -0.372692
Name: sth, dtype: float64 # 使用rename方法
>>s2 = s.rename('dif')
>>s2
0 1.338578
1 2.074678
2 -0.462777
3 0.518763
4 -0.372692
Name: dif, dtype: float64 >>id(s)
2669465319632 >>id(s2)
2669465320416 #s 与 s2是不同的对象,两者尽管值相同,但地址不同
DataFrame 带索引值的二维数组,类似SQL的表,列项通常是不同的数据类型
index 行索引,columns列索引
#使用Series字典或字典创建DataFrame
>>d= {'one':pd.Series([1.,2.,3.], index=['a','b','c']), 'two':pd.Series([1.,2.,3.,4.], index=['a','b','c','d'])}
>>df = pd.DataFrame(d)
>>df
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0 # 按序输出
>>pd.DataFrame(d, index=['d','b','a'])
one two
d NaN 4.0
b 2.0 2.0
a 1.0 1.0 >>df.index
Index(['a', 'b', 'c', 'd'], dtype='object')
>>df.columns
Index(['one', 'two'], dtype='object') #使用ndarrays/list字典
>>d = {'one':[1.,2.,3.,4.],'two':[4.,3.,2.,1.]}
>>pd.DatdFrame(d)
one two
0 1.0 4.0
1 2.0 3.0
2 3.0 2.0
3 4.0 1.0 #指定index
>>pd.DataFrame(d,index=['a','b','c','d'])
one two
a 1.0 4.0
b 2.0 3.0
c 3.0 2.0
d 4.0 1.0
DataFrame操作
列选择、添加、删除
>>df['one']
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64 #添加 three 与 flag 列,总在尾部添加
>>df['three'] = df['one'] * df['two']
>>df['flag']=df['one']>2
>>df
one two three flag
a 1.0 1.0 1.0 False
b 2.0 2.0 4.0 False
c 3.0 3.0 9.0 True
d NaN 4.0 NaN False # 删除
>>del df['two']
>>three = df.pop('three')
>>three
a 1.0
b 4.0
c 9.0
d NaN
Name: three, dtype: float64 >>df
one flag
a 1.0 False
b 2.0 False
c 3.0 True
d NaN False #可以将列数据截断
>>df['one_trunc'] = df['one'][:2]
one flag one_trunc
a 1.0 False 1.0
b 2.0 False 2.0
c 3.0 True NaN
d NaN False NaN >>df['foo'] = 'bar'
>>df
one flag one_trunc foo
a 1.0 False 1.0 bar
b 2.0 False 2.0 bar
c 3.0 True NaN bar
d NaN False NaN bar #使用insert函数可以在指定列后插入
#在第1列后插入
>>df.insert(1,'ba',df['one'])
>>df
one ba flag one_trunc foo
a 1.0 1.0 False 1.0 bar
b 2.0 2.0 False 2.0 bar
c 3.0 3.0 True NaN bar
d NaN NaN False NaN bar
索引、选择行
选择列 df[col] Series
按照标签选择行 df.loc[label] Series
按照索引值选择行 df.iloc[loc] Series
切分行 df[5:10] DataFrame
按照布尔向量选择行 df[bool_vec] DataFrame
>>d = {'one' : pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
'two' : pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
>>df = pd.DataFrame(d)
>>df
one two
a 1.0 1.0
b 2.0 2.0
c 3.0 3.0
d NaN 4.0
#按照标签选择行
>>df.loc['b']
one 2.0
two 2.0
Name: b, dtype: float64
>>type(df.loc['b'])
pandas.core.series.Series
#按照索引值选择行
>>df.iloc[2]
one 3.0
two 3.0
Name: c, dtype: float64
#切分行
>>df[1:3]
one two
b 2.0 2.0
c 3.0 3.0
>>type(df[1:3])
pandas.core.frame.DataFrame
选择列
>>df.one
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64 >>df['one']
a 1.0
b 2.0
c 3.0
d NaN
Name: one, dtype: float64
数据对齐与计算
对齐:列与行标签自动对齐
>>da = pd.DataFrame(np.random.randn(10, 4), columns=['A', 'B', 'C', 'D'])
>>db = pd.DataFrame(np.random.randn(7, 3), columns=['A', 'B', 'C'])
>>da +db
A B C D
0 -0.920370 -0.529455 -2.386419 NaN
1 -1.277148 1.292130 1.196099 NaN
2 1.182199 0.454546 0.381586 NaN
3 1.100170 -1.830894 1.105932 NaN
4 0.507649 1.291516 -2.084368 NaN
5 -1.198811 -2.180978 0.342185 NaN
6 0.667211 2.141364 0.044136 NaN
7 NaN NaN NaN NaN
8 NaN NaN NaN NaN
9 NaN NaN NaN NaN #支持Numpy操作
>>np.exp(da)
>>np.asarray(da)
3维数据类型Penel,在0.20.0及其后续版本中不再支持
新的类型xarray,用于支持多维数据
python数据分析基础——pandas Tutorial的更多相关文章
- Python数据分析基础——Numpy tutorial
参考link https://docs.scipy.org/doc/numpy-dev/user/quickstart.html 基础 Numpy主要用于处理多维数组,数组中元素通常是数字,索引值为 ...
- Python数据分析库pandas基本操作
Python数据分析库pandas基本操作2017年02月20日 17:09:06 birdlove1987 阅读数:22631 标签: python 数据分析 pandas 更多 个人分类: Pyt ...
- Python数据分析之pandas基本数据结构:Series、DataFrame
1引言 本文总结Pandas中两种常用的数据类型: (1)Series是一种一维的带标签数组对象. (2)DataFrame,二维,Series容器 2 Series数组 2.1 Series数组构成 ...
- Numpy使用大全(python矩阵相关运算大全)-Python数据分析基础2
//2019.07.10python数据分析基础——numpy(数据结构基础) import numpy as np: 1.python数据分析主要的功能实现模块包含以下六个方面:(1)numpy—— ...
- Python数据分析基础教程
Python数据分析基础教程(第2版)(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1_FsReTBCaL_PzKhM0o6l0g 提取码:nkhw 复制这段内容后 ...
- Python数据分析基础PDF
Python数据分析基础(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1ImzS7Sy8TLlTshxcB8RhdA 提取码:6xeu 复制这段内容后打开百度网盘手 ...
- Python 数据分析:Pandas 缺省值的判断
Python 数据分析:Pandas 缺省值的判断 背景 我们从数据库中取出数据存入 Pandas None 转换成 NaN 或 NaT.但是,我们将 Pandas 数据写入数据库时又需要转换成 No ...
- python数据分析基础
---恢复内容开始--- Python数据分析基础(1) //2019.07.09python数据分析基础总结1.python数据分析主要使用IDE是Pycharm和Anaconda,最为常用和方便的 ...
- Python数据分析之Pandas操作大全
从头到尾都是手码的,文中的所有示例也都是在Pycharm中运行过的,自己整理笔记的最大好处在于可以按照自己的思路来构建矿建,等到将来在需要的时候能够以最快的速度看懂并应用=_= 注:为方便表述,本章设 ...
随机推荐
- Entity Freamwork CodeFirst 连接PostgreSql数据库
EF的Code First是一个比较强大也比较有用的功能,他可以让你先写代码,最后根据代码去生成数据库,非常符合OO设计的要求,抛开数据库层面不管(当然不是完全的不管),只管对象的设计. 首先,说一下 ...
- unittest单元测试框架之unittest 框架的总结(七)
1. Unittest 是 python 自带的单元测试框架,可以用其作为自动化框架来组织测试用例(测 试用例的执行顺序)的执行. 2. Unittest 框架的流程: 写好 TestCase 通过 ...
- Linux 常用命令整理
系统 切换用户 su 关机/重新启动 shoutdown,reboot,halt,poweroff 内存数据写入磁盘 sync 查询命令用法 "命令 –help" 或 " ...
- 霍金:AI或许能根除疾病和贫穷,但也可能摧毁人类 | GMIC 2017
在我的一生中,我见证了社会深刻的变化.其中最深刻的,同时也是对人类影响与日俱增的变化,是人工智能的崛起.简单来说,我认为强大的人工智能的崛起,要么是人类历史上最好的事,要么是最糟的.我不得不说,是好是 ...
- Oracle子查询之高级子查询
Oracle 高级子查询 高级子查询相对于简单子查询来说,返回的数据行不再是一列,而是多列数据. 1,多列子查询 主查询与子查询返回的多个列进行比较 查询与141号或174号员工的manager_id ...
- java web中对json的使用
一.在Java Web的开发过程中,如果希望调用Java对象转化成JSON对象等操作.则需要引入以下jar包,不然运行时则报错. 1.commons-beanutils.jar 2.commons-c ...
- App 运行后屏幕顶部和底部各留黑边问题 - iOS
App 启动后屏幕的顶部和底部各产生一条黑边,App 的内容会被压缩在两个黑条内显示,比例失调的情况. 初步判断是启动页资源图片适配问题. 首先,查看工程 project => General ...
- npm常见配置收集
npm代理设置为走ss通道:npm config set proxy 'http://localhost:1080'
- JQuery制作网页—— 第六章 jQuery选择器
1.jQuery选择器:jQuery选择器类似于CSS选择器,用来选取网页中的元素. Eg:$("h3").css("background",&qu ...
- 【JavaWeb】从零实现用户登录
1.数据库预备 1.1 SQL 创建数据库 create database db; 创建表 create table userInfo( id int primary key , name ), pa ...