参考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. unittest单元测试框架之测试套件(三)

    1.测试套件(注意:测试用例先添加先执行,后添加后执行,由此组织与设定测试用例的执行顺序) addTests:添加多个测试用例 addTest:添加单个测试用例 import unittest fro ...

  2. 03 Oracle分区表

    Oracle分区表   先说句题外话…   欢迎成都天府软件园的小伙伴来面基交流经验~ 一:什么是分区(Partition)? 分区是将一个表或索引物理地分解为多个更小.更可管理的部分. 分区对应用透 ...

  3. iOS 从零到一搭建组件化项目框架

    随着公司业务需求的不断迭代发展,工程的代码量和业务逻辑也越来越多,原始的开发模式和架构已经无法满足我们的业务发展速度了,这时我们就需要将原始项目进行一次重构大手术了.这时我们应该很清晰这次手术的动刀口 ...

  4. AppleDoc

    使用AppleDoc快速生成iOS开发文档 _ 皮卡丘♪-(´ε` ) 用 appledoc 生成文档 _ Garan no dou xcode-select_ error_ tool 'xcodeb ...

  5. c/c++面试指导---c语法总结

    任何一门学科或者专业在学习的过程中都要把握总结框架,大家在面试c/c++职位过程中要应对各种企业的面试,回答企业面试官的各种技术问题.如何应对各种各样的关于c/c++的企业面试题目,从各种繁杂的题目中 ...

  6. Mysql基于Linux上的安装

    MySQL 在Linux/Unix安装 所有平台的 MySQL 下载地址为: MySQL 下载 . 挑选需要的 MySQL Community Server 版本及对应的平台. 注意:安装过程需要通过 ...

  7. kubernetes基础使用

    查看节点详细信息 kubectl describe node vm2 显示版本号 kubectl version Client Version: version.Info{Major:"1& ...

  8. PHP变量问题,Bugku变量1

    知识点:php正则表达式,php函数,全局变量GLOBALS(注意global和$GLOBALS[]的区别) PHP函数: isset():     条件判断 get方法传递的args参数是否存在 p ...

  9. linux popen 获取 ip test ok

    任务:unix,linux通过c程序获取本机IP. 1. 标准I/O库函数相对于系统调用的函数多了个缓冲区(,buf),安全性上通过buf 防溢出. 2.printf 这类输出函数中“ ”若包含“记得 ...

  10. C语言实现''student a am i''字符串的正确排列

    #define _CRT_SECURE_NO_WARNINGS #include<stdio.h> #include<stdlib.h> int Reverse(char a[ ...