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 ...
随机推荐
- Linux kill/pkill/killall命令详解
kill kill(terminate a process)命令用来终止指定的进程, 对于一个后台进程就须用kill命令来终止,我们就需要先使用ps/pidof/pstree/top等工具获取进程PI ...
- 十分钟带你学会Http协议和Tomcat服务器的原理
1. Http协议 1. 什么是Http协议 HTTP,超文本传输协议(HyperText Transfer Protocol)是互联网上应用最为广泛的一种网络协议.所有的WWW文件都必须遵守这个标准 ...
- Linux下打包压缩war、解压war包和jar命令
情景:把project_a文件夹下的文件打包成project.war 1. 打包 jar -cvf project.war /project_a 说明: -c 创建war包 -v 显示过程信息 -f ...
- oracle给用户分配特定用户下特定表的只读权限
以下是测试过程,测试环境oracle 11.2.0.3 linux平台: 模拟将HR用户下的employees表的只读权限非配给test_ycr创建用户:SQL> create user tes ...
- August 19th 2017 Week 33rd Saturday
Live for today and stop stressing out about tomorrow. 为今天而活,不必为明天过分担心. Stop bewailing the past misfo ...
- centos6.4 minimal 安装kvm
操作系统是网易源下载的centos 64位的minimal安装包,很多工具都没有,像gcc make wget which where 等统统没有,好在有yum 这里为了简单起见直接用yum安装kvm ...
- URAL-1019 Line Painting----暴力或线段树
题目链接: https://cn.vjudge.net/problem/URAL-1019 题目大意: 一个0~1e9的区间,初始都是白的,现进行N次操作,每次将一段区间图上一中颜色.最后问说连续最长 ...
- Java HttpURLConnection模拟请求Rest接口解决中文乱码问题
转自:http://blog.csdn.net/hwj3747/article/details/53635539 在Java使用HttpURLConnection请求rest接口的时候出现了POST请 ...
- Django中模型(五)
Django中模型(五) 六.模型查询 1.概述 查询集,表示从数据库获取的对象集合. 过滤器就是一个函数,基于所给的参数限制查询集结果.查询集可以有多个过滤器. 从sql角度来说,查询集合等价于se ...
- Spring @Value 默认值
@Value(value = "${etl.maxthreadcount:3}") private long MaxThreadCount;