为什么学习pandas

  • numpy已经可以帮助我们进行数据的处理了,那么学习pandas的目的是什么呢?

    • numpy能够帮助我们处理的是数值型的数据,当然在数据分析中除了数值型的数据还有好多其他类型的数据(字符串,时间序列),那么pandas就可以帮我们很好的处理除了数值型的其他数据!

什么是pandas?

  • 首先先来认识pandas中的两个常用的类

    • Series
    • DataFrame

Series

  • Series是一种类似与一维数组的对象,由下面两个部分组成:

    • values:一组数据(ndarray类型)
    • index:相关的数据索引标签
  • Series的创建

    • 由列表或numpy数组创建
    • 由字典创建
import pandas as pd
from pandas import Series,DataFrame
import numpy as np
#Series这个数据结构中存储的数据一定得是一个维度
s1 = Series(data=[1,2,3,4,5]) # 结果如下
0 1
1 2
2 3
3 4
4 5
dtype: int64 s2 = Series(data=np.random.randint(0,100,size=(4,))) # 结果如下
0 54
1 90
2 21
3 92
dtype: int32
# 创建一个字典
dic = {
'a':1,
'b':2,
'c':3
}
#Series的索引可以为字符串
s3 = Series(data=dic) # 结果如下
a 1
b 2
c 3
dtype: int64
#index指定显式索引
s4 = Series(data=[1,2,3],index=['数学','英语','理综']) # 结果如下
数学 1
英语 2
理综 3
dtype: int64
  • Series的索引

    • 隐式索引:数值型
    • 显式所用:自定义(字符串)
      • 提高数据的可读性
  • Series的索引和切片

#索引操作
s4[0]
s4['数学']
s4.数学 # 值为
1
#切片
s4[0:2] # 结果:
数学 1
英语 2
dtype: int64 s4["英语":'语文'] # 结果:
英语 2
语文 3
dtype: int64
  • Series的常用属性

    • shape --------- 查看维度
    • size --------- 查看长度
    • index --------- 查看所有的索引
    • values --------- 查看所有的值
s4.shape
(3,) s4.size
3 s4.index
Index(['数学', '英语', '语文'], dtype='object') s4.values
array([1, 2, 3], dtype=int64)
  • Series的常用方法

    • head(),tail()
    • unique()
    • isnull(),notnull()
    • add() sub() mul() div()
s4.head(2) #显式前n条数据
s4.tail(2) #显式后n条数据 英语 2
理综 3
dtype: int64
s = Series(data=[1,1,2,2,3,4,5,6,6,6,6,6,6,7,8])
s.unique() #对Series进行去重 array([1, 2, 3, 4, 5, 6, 7, 8], dtype=int64)
  • Series的算术运算
s + s
s.add(s) 0 2
1 2
2 4
3 4
4 6
5 8
6 10
7 12
8 12
9 12
10 12
11 12
12 12
13 14
14 16
dtype: int64
  • 算数运算的法则:

    • 索引与之匹配的值进行算数运算,否则补空
s1 = Series(data=[1,2,3,4])
s2 = Series(data=[5,6,7])
s1 + s2 # 结果:
0 6.0
1 8.0
2 10.0
3 NaN
dtype: float64
  • 清洗Series中的空值
s1 = Series(data=[1,2,3,4],index=['a','b','c','e'])
s2 = Series(data=[1,2,3,4],index=['a','d','c','f'])
s = s1 + s2 a 2.0
b NaN
c 6.0
d NaN
e NaN
f NaN
dtype: float64
#boolean可以作为索引取值
s1[[True,False,True,False]] a 1
c 3
dtype: int64
#把s的NAN进行了清洗
s[[True,False,True,False,False,False]] a 2.0
c 6.0
dtype: float64
  • 将符合清洗要求的布尔值获取
s = s1 + s2

a    2.0
b NaN
c 6.0
d NaN
e NaN
f NaN
dtype: float64
s.isnull()

a    False
b True
c False
d True
e True
f True
dtype: bool
s.notnull()

a     True
b False
c True
d False
e False
f False
dtype: bool
#NAN的清洗
s[s.notnull()] a 2.0
c 6.0
dtype: float64

DataFrame

  • DataFrame是一个【表格型】的数据结构。DataFrame由按一定顺序排列的多列数据组成。设计初衷是将Series的使用场景从一维拓展到多维。DataFrame既有行索引,也有列索引。

    • 行索引:index
    • 列索引:columns
    • 值:values
  • DataFrame的创建

    • ndarray创建
    • 字典创建
df = DataFrame(data=np.random.randint(0,100,size=(5,6)))

0 1 2 3 4 5
0 65 84 20 21 82 75
1 92 72 65 88 30 25
2 40 7 28 6 56 70
3 20 77 55 54 87 11
4 89 95 50 22 55 93
dic = {
'name':['tom','jay','bobo'],
'salary':[10000,15000,10000]
}
df = DataFrame(data=dic,index=['a','b','c'])
name salary
a tom 10000
b jay 15000
c bobo 10000
  • DataFrame的属性

    • values、columns、index、shape
df.values      # 查看全部的值
df.shape # 查看结构
df.index # 查看列索引
df.columns # 查看行索引 Index(['name', 'salary'], dtype='object')

============================================

练习4:

根据以下考试成绩表,创建一个DataFrame,命名为df:

    张三  李四
语文 150 0
数学 150 0
英语 150 0
理综 300 0
dic = {
"张三":[150,150,150,300],
"李四":[0,0,0,0]
} df = DataFrame(data=dic,index=["语文","英语","数学","理综"])

============================================

  • DataFrame索引操作

    • 对行进行索引
    • 队列进行索引
    • 对元素进行索引
name salary
a tom 10000
b jay 15000
c bobo 10000
#索引取单列
df['name'] a tom
b jay
c bobo
Name: name, dtype: object
#索引取多列
df[['salary','name']]
salary name
a 10000 tom
b 15000 jay
c 10000 bobo
#取单行
df.loc['a']
df.iloc[0] name tom
salary 10000
Name: a, dtype: object
#取多行
df.loc[['a','c']]
df.iloc[[0,2]]
name salary
a tom 10000
c bobo 10000
df
name salary
a tom 10000
b jay 15000
c bobo 10000
#取单个元素之
df.iloc[1,1]
df.loc['b','salary'] 15000
#取多个元素值
df.loc[['b','c'],'salary']
df.iloc[[1,2],1] b 15000
c 10000
Name: salary, dtype: int64
  • DataFrame的切片操作

    • 对行进行切片
    • 对列进行切片
df
name salary
a tom 10000
b jay 15000
c bobo 10000
#切行
df[0:2]
name salary
a tom 10000
b jay 15000
#切列
df.iloc[:,0:2]
name salary
a tom 10000
b jay 15000

============================================

练习:

  1. 假设ddd是期中考试成绩,ddd2是期末考试成绩,请自由创建ddd2,并将其与ddd相加,求期中期末平均值。

    dic = {
    "ddd":[10,20,30],
    "dd2":[50,60,50]
    } df = DataFrame(data=dic,index=['a','b','c'])
    df.mean(axis=1)
  2. 假设张三期中考试数学被发现作弊,要记为0分,如何实现?

    dic = {
    "张三":[10,20,30],
    } df = DataFrame(data=dic,index=['语文','英语','数学'])
    df
    df.loc["数学"] = 0
    
    df
  3. 李四因为举报张三作弊立功,期中考试所有科目加100分,如何实现?

    dic = {
    "李四":[10,20,30],
    } df = DataFrame(data=dic,index=['语文','英语','数学'])
    df
    df['李四'] += 100
    df
  4. 后来老师发现有一道题出错了,为了安抚学生情绪,给每位学生每个科目都加10分,如何实现?

    dic = {
    "李四":[10,20,30],
    "张三":[10,20,0]
    } df = DataFrame(data=dic,index=['语文','英语','数学'])
    df += 10

============================================

  • 时间数据类型的转换

    • pd.to_datetime(col)
  • 将某一列设置为行索引
    • df.set_index()
dic = {
'time':['2019-01-09','2011-11-11','2018-09-22'],
'salary':[1111,2222,3333]
}
df = DataFrame(data=dic)
salary time
0 1111 2019-01-09
1 2222 2011-11-11
2 3333 2018-09-22
#查看df的数据的数据类型
df.dtypes salary int64
time object
dtype: object
#将time列转换成时间序列类型
df['time'] = pd.to_datetime(df['time'])
salary time
0 1111 2019-01-09
1 2222 2011-11-11
2 3333 2018-09-22
df.dtypes

# 结果
salary int64
time datetime64[ns]
dtype: object
#将time这一列作为原数据的行索引
df.set_index(df['time'],inplace=True)
df.drop(labels='time',axis=1,inplace=True) #drop函数中axis的0行,1列
salary
time
2019-01-09 1111
2011-11-11 2222
2018-09-22 3333

pandas模块的使用详解的更多相关文章

  1. angular-ngSanitize模块-$sanitize服务详解

    本篇主要讲解angular中的$sanitize这个服务.此服务依赖于ngSanitize模块. 要学习这个服务,先要了解另一个指令: ng-bing-html. 顾名思义,ng-bind-html和 ...

  2. angular-ngSanitize模块-linky过滤器详解

    本篇主要讲解angular中的linky这个过滤器.此过滤器依赖于ngSanitize模块. linky能找出文本中的链接,然后把它转换成html链接.什么意思,就是说,一段文本里有一个链接,但是这个 ...

  3. python中argparse模块用法实例详解

    python中argparse模块用法实例详解 这篇文章主要介绍了python中argparse模块用法,以实例形式较为详细的分析了argparse模块解析命令行参数的使用技巧,需要的朋友可以参考下 ...

  4. Python模块调用方式详解

    Python模块调用方式详解 作者:尹正杰 版权声明:原创作品,谢绝转载!否则将追究法律责任. 模块,用一砣代码实现了某个功能的代码集合. 类似于函数式编程和面向过程编程,函数式编程则完成一个功能,其 ...

  5. Nginx RTMP 模块 nginx-rtmp-module 指令详解

    译序:截至 Jul 8th,2013 官方公布的最新 Nginx RTMP 模块 nginx-rtmp-module 指令详解.指令Corertmp语法:rtmp { ... }上下文:根描述:保存所 ...

  6. Python中random模块生成随机数详解

    Python中random模块生成随机数详解 本文给大家汇总了一下在Python中random模块中最常用的生成随机数的方法,有需要的小伙伴可以参考下 Python中的random模块用于生成随机数. ...

  7. webRTC中语音降噪模块ANS细节详解(二)

    上篇(webRTC中语音降噪模块ANS细节详解(一))讲了维纳滤波的基本原理.本篇先给出webRTC中ANS的基本处理过程,然后讲其中两步(即时域转频域和频域转时域)中的一些处理细节. ANS的基本处 ...

  8. webRTC中语音降噪模块ANS细节详解(三)

    上篇(webRTC中语音降噪模块ANS细节详解(二))讲了ANS的处理流程和语音在时域和频域的相互转换.本篇开始讲语音降噪的核心部分,首先讲噪声的初始估计以及基于估计出来的噪声算先验信噪比和后验信噪比 ...

  9. webRTC中语音降噪模块ANS细节详解(四)

    上篇(webRTC中语音降噪模块ANS细节详解(三))讲了噪声的初始估计方法以及怎么算先验SNR和后验SNR. 本篇开始讲基于带噪语音和特征的语音和噪声的概率计算方法和噪声估计更新以及基于维纳滤波的降 ...

随机推荐

  1. 嵌入式开发笔记——调试组件SEGGER_HardFaultHandle

    一.前言 在使用Cortex-M内核的MCU进行开发时,有时候会因为对内存错误访问等原因造成程序产生异常从而进入HardFaultHandler错误中断.如果程序结构比较复杂,尤其是运行了RTOS时可 ...

  2. Python的精髓居然是方括号、花括号和圆括号!

    和其他编程语言相比,什么才是Python独一无二的特色呢?很多程序员会说,缩进.的确,缩进是Python语言的标志性特点,但这只是外在的.形式上的.从语言特性层面讲,Python的特点是什么呢?我尝试 ...

  3. 数组单调性判断以及all和diff函数的用法

    clc;clear all;close all; n = 1 ;x = zeros(100,1);while n~= 0 n = input('请输入向量的长度n(0退出程序):'); for i = ...

  4. Spring Boot 与 Spring MVC到底有什么区别

    前言 Spring 框架就像一个家族,有众多衍生产品例如 boot.security.jpa等等.但他们的基础都是Spring 的 ioc和 aop ioc 提供了依赖注入的容器 aop ,解决了面向 ...

  5. 深入浅出JVM(一):你写得.java文件是如何被加载到内存中执行的

    众所周知,.java文件需要经过编译生成.class文件才能被JVM执行. 其中,JVM是如何加载.class文件,又做了些什么呢? .class文件通过 加载->验证->准备->解 ...

  6. RxHttp 完美适配Android 10/11 上传/下载/进度监听

    1.前言 随着Android 11的正式发布,适配Android 10/11 分区存储就更加的迫切了,因为Android 11开始,将强制开启分区存储,我们就无法再以绝对路径的方式去读写非沙盒目录下的 ...

  7. maven 父子项目 pom

    父项目 <!--项目 id--> <artifactId>expressway-cloud-travel</artifactId> <!-- 子模块--> ...

  8. 如何解决Renesas USB3.0RootHub警告

    打开WINDOWS系统的[计算机管理]-[服务和应用程序]-[服务]-点击[Portable Device Enumerator Service]服务,设置为启动类型:自动(延迟启动).并点击&quo ...

  9. devops持续集成

    目录 Devops 版本控制系统 Git安装 Git使用 git四种状态 git基础命令 git分支 git合并冲突 git标签 git命令总结 Github使用 创建仓库 HTTP协议 SSH协议 ...

  10. 论文阅读 Characterization of Multiple 3D LiDARs for Localization and Mapping using Normal Distributions Transform

    Abstract 在这个文章里, 我们细致的比较了10种不同的3D LiDAR传感器, 用了一般的 Normal Distributions Transform (NDT) 算法. 我们按以下几个任务 ...