教你十分钟学会使用pandas。

pandas是python数据分析的一个最重要的工具。

基本使用

# 一般以pd作为pandas的缩写
import pandas as pd # 读取文件
df = pd.read_csv('file.csv') # 返回数据的大小
df.shape # 显示数据的一些对象信息和内存使用
df.info() # 显示数据的统计量信息
df.describe()

花式索引

我们的主要数据结构就是DataFrame了,DataFrame有两部分构成,一个是列(columns)。列是有名称的或者说有标签的。另一个是索引(index),这里我们为了避孕歧义称之为行(rows),行一般没有名称,但是也可以有名称。

如图所示:

data = {'animal': ['cat', 'cat', 'snake', 'dog', 'dog', 'cat', 'snake', 'cat', 'dog', 'dog'],
'age': [2.5, 3, 0.5, np.nan, 5, 2, 4.5, np.nan, 7, 3],
'visits': [1, 3, 2, 3, 2, 3, 1, 1, 2, 1],
'priority': ['yes', 'yes', 'no', 'yes', 'no', 'no', 'no', 'yes', 'no', 'no']} labels = ['a', 'b', 'c', 'd', 'e', 'f', 'g', 'h', 'i', 'j'] df = pd.DataFrame(data, index=labels)
>>> df
age animal priority visits
a 2.5 cat yes 1
b 3.0 cat yes 3
c 0.5 snake no 2
d NaN dog yes 3
e 5.0 dog no 2
f 2.0 cat no 3
g 4.5 snake no 1
h NaN cat yes 1
i 7.0 dog no 2
j 3.0 dog no 1

原始索引

原始索引就是类list的索引方式。

当索引对象是切片时就是行索引。

>>> df[1:3]
age animal priority visits
b 3.0 cat yes 3
c 0.5 snake no 2

当索引对象是list时就是列索引。

>>> df[['age', 'animal']]
age animal
a 2.5 cat
b 3.0 cat
c 0.5 snake
d NaN dog
e 5.0 dog
f 2.0 cat
g 4.5 snake
h NaN cat
i 7.0 dog
j 3.0 dog

跟上面等效,上面是用了列名称,这里用了列序号。

>>> df[[0,1]]
age animal
a 2.5 cat
b 3.0 cat
c 0.5 snake
d NaN dog
e 5.0 dog
f 2.0 cat
g 4.5 snake
h NaN cat
i 7.0 dog
j 3.0 dog

位置索引

>>> df.iloc[0:2, 0:2]
age animal
a 2.5 cat
b 3.0 cat

标签索引

lociloc的主要区别就是索引要用标签不能用序号。

>>> df.loc[['a', 'b'], ['animal', 'age']]
animal age
a cat 2.5
b cat 3.0

混合索引

其实就是位置索引和标签索引的混合使用方式。

>>> df.ix[0:2, ['animal', 'age']]
animal age
a cat 2.5
b cat 3.0

条件索引

>>> df[(df['animal'] == 'cat') & (df['age'] < 3)]
age animal priority visits
a 2.5 cat yes 1
f 2.0 cat no 3

数据清洗

找到缺失值。

>>> df[df['age'].isnull()]
age animal priority visits
d NaN dog yes 3
h NaN cat yes 1

填充缺失值。

>>> df['age'].fillna(0, inplace=True)
>>> df
age animal priority visits
a 2.5 cat yes 1
b 3.0 cat yes 3
c 0.5 snake no 2
d 0.0 dog yes 3
e 5.0 dog no 2
f 2.0 cat no 3
g 4.5 snake no 1
h 0.0 cat yes 1
i 7.0 dog no 2
j 3.0 dog no 1

将字符值替换成布尔值

>>> df['priority'] = df['priority'].map({'yes': True, 'no': False})
>>> df
age animal priority visits
a 2.5 cat True 1
b 3.0 cat True 3
c 0.5 snake False 2
d 0.0 dog True 3
e 5.0 dog False 2
f 2.0 cat False 3
g 4.5 snake False 1
h 0.0 cat True 1
i 7.0 dog False 2
j 3.0 dog False 1

速查表

练习

老样子,来写点习题吧。

100道pandas练习题

pandas练习库

参考

官方版十分钟入门pandas

pandas cookbook

最新文章

  1. UWP学习目录整理
  2. 无法向会话状态服务器发出会话状态请求。请确保 ASP.NET State Service (ASP.NET 状态服务)已启动,并且客户端端口与服务器端口相同。如果服务器位于远程计算机上,请检查。。。
  3. ARMLinux下Alignment trap的一些测试 【转自 李迟的专栏 CSDN http://blog.csdn.net/subfate/article/details/7847356
  4. 【Mocha.js 101】同步、异步与 Promise
  5. SCC重新建图
  6. php随机生成指定长度的字符串 可以固定数字 字母 混合
  7. 什么是SQL注入式攻击
  8. ACM题目————已知前序和中序求后序
  9. SQL 语句转换格式函数Cast、Convert 区别
  10. Spring框架学习之第7节
  11. 【转】Angularjs Controller 间通信机制
  12. AspectJ给类的属性打桩,进行替换。
  13. -eq、-ne、-gt、-ge、-lt、-le英文意思
  14. Red and Black---POJ - 1979
  15. Android DatePickerDialog 使用方法
  16. Redis列表操作
  17. OC中Runtime浅析
  18. nmcli 使用记录---fatt
  19. css消除空白节点的方法
  20. 64_g1

热门文章

  1. Windows如何利用输入法简单的打出 ‘↑’ ‘↓’ ‘↖’等箭头
  2. 牛客网Java刷题知识点之Java为什么不能支持多继承,但可以用接口来间接实现多继承
  3. vue中的number
  4. 20 P2678 跳石头
  5. centos 7 安装JDK (Linux安装jdk)
  6. 中国区 Azure 应用程序开发说明
  7. 电脑连接海信电视 HDMI
  8. Windows64+Python27下配置matplotlib
  9. codevs 2277 爱吃皮蛋的小明(水题日常)
  10. -[UPAInitViewController startAPPay] in libUPAPayPlugin.a(UPAInitViewController.o)