python学习笔记(四):pandas基础
pandas 基础
serise
import pandas as pd
from pandas import Series, DataFrame
obj = Series([4, -7, 5, 3])
obj
0 4
1 -7
2 5
3 3
dtype: int64
obj.values
array([ 4, -7, 5, 3], dtype=int64)
obj.index
RangeIndex(start=0, stop=4, step=1)
obj[[1,3]]
# 跳着选取数据
1 -7
3 3
dtype: int64
obj[1:3]
1 -7
2 5
dtype: int64
pd.isnull(obj)
0 False
1 False
2 False
3 False
dtype: bool
- reindex可以用来插值
obj.reindex(range(5), method = 'ffill')
0 4
1 -7
2 5
3 3
4 3
dtype: int64
- 标签切片是闭区间的
dataframe
data = {'state': ['asd','qwe','sdf','ert'],
'year': [2000, 2001, 2002, 2003],
'pop': [1.5,1.7,3.6,2.4]}
data = DataFrame(data)
data
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | state | year | |
|---|---|---|---|
| 0 | 1.5 | asd | 2000 |
| 1 | 1.7 | qwe | 2001 |
| 2 | 3.6 | sdf | 2002 |
| 3 | 2.4 | ert | 2003 |
data.year
# 比r里提取列要方便点
0 2000
1 2001
2 2002
3 2003
Name: year, dtype: int64
data['debt'] = range(4)
data
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | state | year | debt | |
|---|---|---|---|---|
| 0 | 1.5 | asd | 2000 | 0 |
| 1 | 1.7 | qwe | 2001 | 1 |
| 2 | 3.6 | sdf | 2002 | 2 |
| 3 | 2.4 | ert | 2003 | 3 |
- index是不能修改的
a = data.index
a[1] = 6
---------------------------------------------------------------------------
TypeError Traceback (most recent call last)
<ipython-input-9-57677294f950> in <module>()
1 a = data.index
----> 2 a[1] = 6
F:\Anaconda\lib\site-packages\pandas\core\indexes\base.py in __setitem__(self, key, value)
1668
1669 def __setitem__(self, key, value):
-> 1670 raise TypeError("Index does not support mutable operations")
1671
1672 def __getitem__(self, key):
TypeError: Index does not support mutable operations
data.columns
Index(['pop', 'state', 'year', 'debt'], dtype='object')
- .ix标签索引功能,输入行和列
- 不加.ix只能选取其中的某列或某行,不能列与行同时选取
data[:3]
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | state | year | debt | |
|---|---|---|---|---|
| 0 | 1.5 | asd | 2000 | 0 |
| 1 | 1.7 | qwe | 2001 | 1 |
| 2 | 3.6 | sdf | 2002 | 2 |
data.ix[:,:3]
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | state | year | |
|---|---|---|---|
| 0 | 1.5 | asd | 2000 |
| 1 | 1.7 | qwe | 2001 |
| 2 | 3.6 | sdf | 2002 |
| 3 | 2.4 | ert | 2003 |
- 删除某列用drop,axis = 0表示行,1表示列
- 删除后原数据不变
data.drop(0,axis=0)
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | state | year | debt | |
|---|---|---|---|---|
| 1 | 1.7 | qwe | 2001 | 1 |
| 2 | 3.6 | sdf | 2002 | 2 |
| 3 | 2.4 | ert | 2003 | 3 |
data.drop('year', axis=1)
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | state | debt | |
|---|---|---|---|
| 0 | 1.5 | asd | 0 |
| 1 | 1.7 | qwe | 1 |
| 2 | 3.6 | sdf | 2 |
| 3 | 2.4 | ert | 3 |
data
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | state | year | debt | |
|---|---|---|---|---|
| 0 | 1.5 | asd | 2000 | 0 |
| 1 | 1.7 | qwe | 2001 | 1 |
| 2 | 3.6 | sdf | 2002 | 2 |
| 3 | 2.4 | ert | 2003 | 3 |
import numpy as np
df = DataFrame(np.arange(9).reshape(3, 3))
df
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 0 | 1 | 2 |
| 1 | 3 | 4 | 5 |
| 2 | 6 | 7 | 8 |
- applymap()可以对dataframe每一个元素运用函数
- apply()可以对每一维数组运用函数
df.applymap(lambda x: '%.2f' % x)
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 0.00 | 1.00 | 2.00 |
| 1 | 3.00 | 4.00 | 5.00 |
| 2 | 6.00 | 7.00 | 8.00 |
data.sort_values(by='pop')
# 对某一列排序
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | state | year | debt | |
|---|---|---|---|---|
| 0 | 1.5 | asd | 2000 | 0 |
| 1 | 1.7 | qwe | 2001 | 1 |
| 3 | 2.4 | ert | 2003 | 3 |
| 2 | 3.6 | sdf | 2002 | 2 |
data.describe()
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| pop | year | debt | |
|---|---|---|---|
| count | 4.000000 | 4.000000 | 4.000000 |
| mean | 2.300000 | 2001.500000 | 1.500000 |
| std | 0.948683 | 1.290994 | 1.290994 |
| min | 1.500000 | 2000.000000 | 0.000000 |
| 25% | 1.650000 | 2000.750000 | 0.750000 |
| 50% | 2.050000 | 2001.500000 | 1.500000 |
| 75% | 2.700000 | 2002.250000 | 2.250000 |
| max | 3.600000 | 2003.000000 | 3.000000 |
df.isin([1])
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | False | True | False |
| 1 | False | False | False |
| 2 | False | False | False |
- None、NaN会被当作NA处理
- df.shape不加括号相当于dim()
df.shape
(3, 3)
- dropna删除缺失值
df.ix[:1, :1] = None
df
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | NaN | NaN | 2 |
| 1 | NaN | NaN | 5 |
| 2 | 6.0 | 7.0 | 8 |
- 填充缺失值可以调用字典,不同行添加不同值
df.fillna({0:11, 1:22})
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 11.0 | 22.0 | 2 |
| 1 | 11.0 | 22.0 | 5 |
| 2 | 6.0 | 7.0 | 8 |
df
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | NaN | NaN | 2 |
| 1 | NaN | NaN | 5 |
| 2 | 6.0 | 7.0 | 8 |
df.fillna({0:11, 1:22}, inplace=True)
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 11.0 | 22.0 | 2 |
| 1 | 11.0 | 22.0 | 5 |
| 2 | 6.0 | 7.0 | 8 |
df
.dataframe thead tr:only-child th {
text-align: right;
}
.dataframe thead th {
text-align: left;
}
.dataframe tbody tr th {
vertical-align: top;
}
| 0 | 1 | 2 | |
|---|---|---|---|
| 0 | 11.0 | 22.0 | 2 |
| 1 | 11.0 | 22.0 | 5 |
| 2 | 6.0 | 7.0 | 8 |
- inplace修改对象不产生副本
python学习笔记(四):pandas基础的更多相关文章
- python学习笔记四 迭代器,生成器,装饰器(基础篇)
迭代器 __iter__方法返回一个迭代器,它是具有__next__方法的对象.在调用__next__方法时,迭代器会返回它的下一个值,若__next__方法调用迭代器 没有值返回,就会引发一个Sto ...
- Python学习笔记一(基础信息)
目录 输入输出 数据类型和变量 整数 浮点数 字符串 布尔值 空值 变量 常量 小结 欢迎关注我的博客我在马路边 说明:此笔记不是从零开始,在学习的过程中感觉需要记录一些比较重要和需要重复浏览的信息, ...
- PYTHON 爬虫笔记四:正则表达式基础用法
知识点一:正则表达式详解及其基本使用方法 什么是正则表达式 正则表达式对子符串操作的一种逻辑公式,就是事先定义好的一些特定字符.及这些特定字符的组合,组成一个‘规则字符串’,这个‘规则字符串’用来表达 ...
- 吴裕雄--python学习笔记:爬虫基础
一.什么是爬虫 爬虫:一段自动抓取互联网信息的程序,从互联网上抓取对于我们有价值的信息. 二.Python爬虫架构 Python 爬虫架构主要由五个部分组成,分别是调度器.URL管理器.网页下载器.网 ...
- Python学习笔记(四)Python函数的参数
Python的函数除了正常使用的必选参数外,还可以使用默认参数.可变参数和关键字参数. 默认参数 基本使用 默认参数就是可以给特定的参数设置一个默认值,调用函数时,有默认值得参数可以不进行赋值,如: ...
- Java基础学习笔记四 Java基础语法
数组 数组的需求 现在需要统计某公司员工的工资情况,例如计算平均工资.最高工资等.假设该公司有50名员工,用前面所学的知识完成,那么程序首先需要声明50个变量来分别记住每位员工的工资,这样做会显得很麻 ...
- Python学习笔记四
一.装饰器 1.知识储备 函数对象 函数可以被引用 函数可以当参数传递 返回值可以是函数 可以当作容器的元素 def func1(): print (666) def func2(): print ( ...
- Python学习笔记四:面向对象编程
一:定义类并创建实例 Python中定义类,通过class关键字,类名开头大写,参数列表为所继承的父类.如果没有需要明确继承的类,则继承object. 使用类来创建对象,只需 类名+() 形式即可,p ...
- python学习笔记(四) 思考和准备
一.zip的坑 zip()函数接收多个可迭代数列,将数列中的元素重新组合,在3.0中返回迭代器指向 数列首地址,在3.0以下版本返回List类型的列表数列.我用的是3.5版本python, 所以zip ...
- 【Python学习笔记】Pandas库之DataFrame
1 简介 DataFrame是Python中Pandas库中的一种数据结构,它类似excel,是一种二维表. 或许说它可能有点像matlab的矩阵,但是matlab的矩阵只能放数值型值(当然matla ...
随机推荐
- HBase编程 API入门系列之create(管理端而言)(8)
大家,若是看过我前期的这篇博客的话,则 HBase编程 API入门系列之put(客户端而言)(1) 就知道,在这篇博文里,我是在HBase Shell里创建HBase表的. 这里,我带领大家,学习更高 ...
- HDU 1542 矩形面积并
推荐阅读这篇文章 这里仅根据上述文章进行一些补充.主要是注意这里线段树点的意义变成了“边”.比如+-----+-----+,看作3个点abc和两条边e1和e2,那么线段树中点a代表e1,点b代表e2. ...
- svg压缩工具svgo安装使用
svgo是基于node.js的插件,所以需要先安装node.js 1.安装完node.js后,打开node.js命令窗口,输入npm install -g svgo,安装成后会出现下边的内容 2.对s ...
- 问题:alias设置与删除
新建alias条目 临时 alias monitor='gnome-system-monitor' 永久 可以在家目录下,新建 .bash_aliases 文件,然后在其中加上你想要的替换的比 ...
- http协议要素
1)网络节点: 2)通信信道: 3)操作命令: 4)数据载体:
- 2424. [HAOI2010]订货【费用流】
Description 某公司估计市场在第i个月对某产品的需求量为Ui,已知在第i月该产品的订货单价为di,上个月月底未销完的单位产品要付存贮费用m,假定第一月月初的库存量为零,第n月月底的库存量也为 ...
- 图论——最短路径 Dijkstra算法、Floyd算法
1.弗洛伊德算法(Floyd) 弗洛伊算法核心就是三重循环,M [ j ] [ k ] 表示从 j 到 k 的路径,而 i 表示当前 j 到 k 可以借助的点:红色部分表示,如果 j 到 i ,i 到 ...
- 随手练—— 洛谷-P2945 Sand Castle(贪心)
题目链接:https://www.luogu.org/problemnew/show/P2945 (原题 USACO) 要求钱最少,就是试着让M和B的离散程度最小(我自己脑补的,就是总体更接近,我不知 ...
- 五子棋AI算法
原理框图总结 参考链接 http://blog.csdn.net/xiaoyu714543065/article/details/8746876 http://blog.csdn.net/pi9nc/ ...
- textarea多行文本框自适应高度
<script src="https://cdn.bootcss.com/jquery/1.8.1/jquery.js"></script> <scr ...