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 ...
随机推荐
- webpack笔记三 管理输出
webpack笔记三 管理输出 增加src/print.js: export default function printMe() { console.log('I get called from p ...
- 原生ajax和jsonp
封装方法: function ajax(options) { options = options || {}; options.type = (options.type || "GET&qu ...
- android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升
android 智能指针的学习先看邓凡平的书扫盲 再看前面两片博客提升
- [BZOJ 2730][HNOI 2012] 矿场搭建
2730: [HNOI2012]矿场搭建 Time Limit: 10 Sec Memory Limit: 128 MBSubmit: 2113 Solved: 979[Submit][Statu ...
- Java 集合框架(常用数据结构)
早在Java 2中之前,Java就提供了特设类.比如:向量(Vector).栈(Stack).字典(Dictionary).哈希表(Hashtable)这些类(数据结构)用来存储和操作对象组.虽然这些 ...
- 1、关于python第三方工具操作xls和xlsx格式的excel文档选型的吐血经历
首先,最近看了python的一本书,其中第7章是关于文章操作的,就计划把python操作excel,word,txt,xml,html,json等格式的文档做个总结,并实现一些功能,但是,第一步就要把 ...
- HTML的CoreText流畅度超过WebView。CoreText第三方框架DTCoreText的介绍
为什么要用CoreText(富文本)来取代WebView去显示内容.主要的原因就WebView有很大的问题,性能,FPS,卡顿,与原生不搭.这些都是大问题. WebView的缺点 1.直接使用WebV ...
- swift 第一个IOS应用程序
swift 出来也有一阵子了,一直没有时间来研究.简单的看了看.随手写几篇文章.特此声明:本博客纯属个人学习,有不足之处,属于正常,希望多多见谅. 第一个IOS应用程序开发 一.准备工作: (1)Ma ...
- vue2.* 环境搭建01
搭建vue的开发环境: https://cn.vuejs.org/v2/guide/installation.html 1.必须要安装nodejs 2.搭建vue的开发环境 ,安装vue的脚手架工具 ...
- Freeze partial parameters while training
1. requires_grad = False Set all parameters in the current model frozen: for p in self.parameters(): ...