参考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的更多相关文章

  1. Python数据分析基础——Numpy tutorial

    参考link  https://docs.scipy.org/doc/numpy-dev/user/quickstart.html 基础 Numpy主要用于处理多维数组,数组中元素通常是数字,索引值为 ...

  2. Python数据分析库pandas基本操作

    Python数据分析库pandas基本操作2017年02月20日 17:09:06 birdlove1987 阅读数:22631 标签: python 数据分析 pandas 更多 个人分类: Pyt ...

  3. Python数据分析之pandas基本数据结构:Series、DataFrame

    1引言 本文总结Pandas中两种常用的数据类型: (1)Series是一种一维的带标签数组对象. (2)DataFrame,二维,Series容器 2 Series数组 2.1 Series数组构成 ...

  4. Numpy使用大全(python矩阵相关运算大全)-Python数据分析基础2

    //2019.07.10python数据分析基础——numpy(数据结构基础) import numpy as np: 1.python数据分析主要的功能实现模块包含以下六个方面:(1)numpy—— ...

  5. Python数据分析基础教程

    Python数据分析基础教程(第2版)(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1_FsReTBCaL_PzKhM0o6l0g 提取码:nkhw 复制这段内容后 ...

  6. Python数据分析基础PDF

    Python数据分析基础(高清版)PDF 百度网盘 链接:https://pan.baidu.com/s/1ImzS7Sy8TLlTshxcB8RhdA 提取码:6xeu 复制这段内容后打开百度网盘手 ...

  7. Python 数据分析:Pandas 缺省值的判断

    Python 数据分析:Pandas 缺省值的判断 背景 我们从数据库中取出数据存入 Pandas None 转换成 NaN 或 NaT.但是,我们将 Pandas 数据写入数据库时又需要转换成 No ...

  8. python数据分析基础

    ---恢复内容开始--- Python数据分析基础(1) //2019.07.09python数据分析基础总结1.python数据分析主要使用IDE是Pycharm和Anaconda,最为常用和方便的 ...

  9. Python数据分析之Pandas操作大全

    从头到尾都是手码的,文中的所有示例也都是在Pycharm中运行过的,自己整理笔记的最大好处在于可以按照自己的思路来构建矿建,等到将来在需要的时候能够以最快的速度看懂并应用=_= 注:为方便表述,本章设 ...

随机推荐

  1. 【oracle笔记1】基础知识大集锦:增删改,数据类型,用户操作,持续更新中···

    什么是数据库?数据库就是用来存储和管理数据的仓库.首先我来简单介绍一下各数据库的背景,常见的数据库如下,oracle:甲骨文公司(市场占用率最高),oracle也是一个公司名,翻译过来就是甲骨文的意思 ...

  2. #leetcode刷题之路37-解数独

    编写一个程序,通过已填充的空格来解决数独问题.一个数独的解法需遵循如下规则:数字 1-9 在每一行只能出现一次.数字 1-9 在每一列只能出现一次.数字 1-9 在每一个以粗实线分隔的 3x3 宫内只 ...

  3. 转:30分钟学会如何使用Shiro

    引自:http://www.cnblogs.com/learnhow/p/5694876.html 本篇内容大多总结自张开涛的<跟我学Shiro>原文地址:http://jinniansh ...

  4. ie 8在打印网页的时候打印预览是空白的

    win 7专业版系统中的ie 8在打印网页的时候打印预览是空白的,打印出来也是空白的,但是用别的浏览器打印没有问题 根据您的描述,该问题主要是由于保护模式下%Temp%\Low不正常工作引起的. 建议 ...

  5. Win7装在其他盘 (非C盘)办法

    Win7装在其他盘 (非C盘)办法 1]将GHO还原到其他盘(非C盘),如H盘 2]用进U盘系统,里的工具,恢复启动H盘 3]将H盘的Boot文件夹,及其他根目录的所有文件复制到C盘根目录,重启即可开 ...

  6. Java使用zxing生成解读QRcode二维码

    1.maven的pom配置jar包,如果不实用maven请手动下载jar包 <dependency> <groupId>com.google.zxing</groupId ...

  7. Arduino UNO仿真开发环境设置和仿真运行

    一. Proteus仿真平台简介 Proteus软件是英国Labcenter electronics公司出版的EDA工具软件(该软件中国总代理为广州风标电子技术有限公司).它不仅具有其它EDA工具软件 ...

  8. 『Python题库 - 简答题』 Python中的基本概念 (121道)

    ## 『Python题库 - 简答题』 Python中的基本概念 1. Python和Java.PHP.C.C#.C++等其他语言的对比? 2. 简述解释型和编译型编程语言? 3. 代码中要修改不可变 ...

  9. Blender2.79建模快捷键

    快捷键 基本操作 滚动鼠标中键滚轮:视图放大或缩小 按住鼠标中键滚轮:视图旋转 单独鼠标右键:选择物体 单独鼠标右键:放置物体 shift+鼠标中键:视图平移 小键盘数字1:前视图:ctrl+1:后视 ...

  10. UNIX故障--sun m4000服务器故障硬盘更换案例

    一.故障诊断 查看messages日志c0d0t0这块盘不断报错,类型为:retryable,如下: root@gdhx # more /var/adm/messages Aug  5 16:43:0 ...