快速入门Pandas
教你十分钟学会使用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
标签索引
loc与iloc的主要区别就是索引要用标签不能用序号。
>>> 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
快速入门Pandas的更多相关文章
- 快速入门 Pandas
先po几个比较好的Pandas入门网站十分钟入门:http://www.codingpy.com/article/a-quick-intro-to-pandas/手册前2章:http://pda.re ...
- pandas快速入门
pandas快速入门 numpy之后让我们紧接着学习pandas.Pandas最初被作为金融数据分析工具而开发出来,后来因为其强大性以及友好性,在数据分析领域被广泛使用,下面让我们一窥究竟. 本文参考 ...
- Python pandas快速入门
Python pandas快速入门2017年03月14日 17:17:52 青盏 阅读数:14292 标签: python numpy 数据分析 更多 个人分类: machine learning 来 ...
- Pandas 快速入门(二)
本文的例子需要一些特殊设置,具体可以参考 Pandas快速入门(一) 数据清理和转换 我们在进行数据处理时,拿到的数据可能不符合我们的要求.有很多种情况,包括部分数据缺失,一些数据的格式不正确,一些数 ...
- Jupyter 快速入门——写python项目博客非常有用!!!
from:https://blog.csdn.net/m0_37338590/article/details/78862488 一.简介: Jupyter Notebook(此前被称为 IPython ...
- python快速入门——进入数据挖掘你该有的基础知识
这篇文章是用来总结python中重要的语法,通过这些了解你可以快速了解一段python代码的含义 Python 的基础语法来带你快速入门 Python 语言.如果你想对 Python 有全面的了解请关 ...
- 快速入门 Python 数据分析实用指南
Python 现如今已成为数据分析和数据科学使用上的标准语言和标准平台之一.那么作为一个新手小白,该如何快速入门 Python 数据分析呢? 下面根据数据分析的一般工作流程,梳理了相关知识技能以及学习 ...
- 数据分析入门——pandas之DataFrame基本概念
一.介绍 数据帧(DataFrame)是二维数据结构,即数据以行和列的表格方式排列. 可以看作是Series的二维拓展,但是df有行列索引:index.column 推荐参考:https://www. ...
- 数据分析入门——pandas之Series
一.介绍 Pandas是一个开源的,BSD许可的库(基于numpy),为Python编程语言提供高性能,易于使用的数据结构和数据分析工具. 官方中文文档:https://www.pypandas.cn ...
随机推荐
- Codeforces 1139E(二分图最大匹配)
pi只有0-5000且只找最小的没出现的,又要找不同club的,考虑二分匹配,左边pi,右边ci,一个匹配一个.离线倒着加边即可. const int maxn = 5e3 + 5; int m, n ...
- bryce1010专题训练——CDQ分治
Bryce1010模板 CDQ分治 1.与普通分治的区别 普通分治中,每一个子问题只解决它本身(可以说是封闭的) 分治中,对于划分出来的两个子问题,前一个子问题用来解决后一个子问题而不是它本身 2.试 ...
- Retrofit实现Delete请求
//设置取消关注 @Headers("Content-Type:application/x-www-form-urlencoded") @HTTP(method = "D ...
- 持续集成~Jenkins里的NuGet和MSBuild插件
Jenkins是一个持续集成的环境,它是java开发的,大叔认为它的工作流程是 从源代码拉一个项目下来到它本地(可以配置定时机制) 恢复相关程序包nuget 编译程序 发布程序 现在说一下在配置jen ...
- Unity Shader入门精要学习笔记 - 第9章 更复杂的光照
转载自 冯乐乐的<Unity Shader入门精要> Unity 的渲染路径 在Unity里,渲染路径决定了光照是如何应该到Unity Shader 中的.因此,如果要和光源打交道,我们需 ...
- js获取select下拉框选项的值
var onchange="getBatch(this.options[this.options.selectedIndex].value)"
- 【Linux】Tmux分屏
1.Tmux Arch维基: https://wiki.archlinux.org/index.php/Tmux_(简体中文) 官方WIKI: https://github.com/tmux/tmux ...
- Ubuntu 16.04 not a com32r image
安装Ubuntu16.04,出现题目中的错误,解决方法如下 重点:开机后按TAB键,在随后出现的命令行提示符中输入live 既可,之后的过程就是正常的过程了!
- MySQL主从复制原理介绍
1)在mysql主库上,将改变记录到二进制日志(binary log)中. 2)在mysql从库上,IO线程将mysql主库上二进制日志(binary log)复制到中继日志(replay log)中 ...
- swift 接水果游戏ios源码
初学swift,写来练手的,游戏很简单 ,顾名思义就是接水果 ,菠萝不能接,接到一个水果得一分,接到菠萝扣五分,漏一个水果扣一分,初始分0分,当分数低于0分 就Game Over了,暂时适用5s的模拟 ...