pandas DataFrame.shift()函数可以把数据移动指定的位数

period参数指定移动的步幅,可以为正为负.axis指定移动的轴,1为行,0为列.

eg: 有这样一个DataFrame数据:

import pandas as pd
data1 = pd.DataFrame({
'a': [0, 1, 2, 3, 4, 5, 6, 7, 8, 9],
'b': [9, 8, 7, 6, 5, 4, 3, 2, 1, 0]
})
print data1
   a  b
0 0 9
1 1 8
2 2 7
3 3 6
4 4 5
5 5 4
6 6 3
7 7 2
8 8 1
9 9 0

如果想让 a和b的数据都往下移动一位:

data2 = data1.shift(axis=0)
print data2
     a    b
0 NaN NaN
1 0.0 9.0
2 1.0 8.0
3 2.0 7.0
4 3.0 6.0
5 4.0 5.0
6 5.0 4.0
7 6.0 3.0
8 7.0 2.0
9 8.0 1.0

如果是在行上往右移动一位:

data3 = data1.shift(axis=1)
print data3
    a    b
0 NaN 0.0
1 NaN 1.0
2 NaN 2.0
3 NaN 3.0
4 NaN 4.0
5 NaN 5.0
6 NaN 6.0
7 NaN 7.0
8 NaN 8.0
9 NaN 9.0

如果想往上或者往左移动,可以指定(periods=-1):

data4 = data1.shift(periods=-1, axis=0)
print data4
     a    b
0 1.0 8.0
1 2.0 7.0
2 3.0 6.0
3 4.0 5.0
4 5.0 4.0
5 6.0 3.0
6 7.0 2.0
7 8.0 1.0
8 9.0 0.0
9 NaN NaN

一个例子:

这里有一组某车站各个小时的总进站人数和总出站人数的数据:

entries_and_exits = pd.DataFrame({
'ENTRIESn': [3144312, 3144335, 3144353, 3144424, 3144594,
3144808, 3144895, 3144905, 3144941, 3145094],
'EXITSn': [1088151, 1088159, 1088177, 1088231, 1088275,
1088317, 1088328, 1088331, 1088420, 1088753]
})

要求计算每个小时该车站进出站人数

思路: 把第n+1小时的总人数-第n小时的总人数,就是这个小时里的进出站人数

entries_and_exits_hourly = entries_and_exits - entries_and_exits.shift(axis=0)print(entries_and_exits_hourly.fillna(0))   #最后用0来填补NaN
   ENTRIESn  EXITSn
0 0.0 0.0
1 23.0 8.0
2 18.0 18.0
3 71.0 54.0
4 170.0 44.0
5 214.0 42.0
6 87.0 11.0
7 10.0 3.0
8 36.0 89.0
9 153.0 333.0

pandas DataFrame.shift()函数的更多相关文章

  1. pandas DataFrame apply()函数(1)

    之前已经写过pandas DataFrame applymap()函数 还有pandas数组(pandas Series)-(5)apply方法自定义函数 pandas DataFrame 的 app ...

  2. pandas DataFrame apply()函数(2)

    上一篇pandas DataFrame apply()函数(1)说了如何通过apply函数对DataFrame进行转换,得到一个新的DataFrame. 这篇介绍DataFrame apply()函数 ...

  3. pandas DataFrame applymap()函数

    pandas DataFrame的 applymap() 函数可以对DataFrame里的每个值进行处理,然后返回一个新的DataFrame: import pandas as pd df = pd. ...

  4. [Python Study Notes]pandas.DataFrame.plot()函数绘图

    ''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''''' ...

  5. python重要的第三方库pandas模块常用函数解析之DataFrame

    pandas模块常用函数解析之DataFrame 关注公众号"轻松学编程"了解更多. 以下命令都是在浏览器中输入. cmd命令窗口输入:jupyter notebook 打开浏览器 ...

  6. Lesson4——Pandas DataFrame结构

    pandas目录 思维导图 1 简介 DataFrame 是 Pandas 的重要数据结构之一,也是在使用 Pandas 进行数据分析过程中最常用的结构之一. 2 认识DataFrame结构 Data ...

  7. pandas.DataFrame的pivot()和unstack()实现行转列

    示例: 有如下表需要进行行转列: 代码如下: # -*- coding:utf-8 -*- import pandas as pd import MySQLdb from warnings impor ...

  8. 把pandas dataframe转为list方法

    把pandas dataframe转为list方法 先用numpy的 array() 转为ndarray类型,再用tolist()函数转为list

  9. Pandas Dataframe增、删、改、查、去重、抽样基本操作

    总括 pandas的索引函数主要有三种: loc 标签索引,行和列的名称 iloc 整型索引(绝对位置索引),绝对意义上的几行几列,起始索引为0 ix 是 iloc 和 loc的合体 at是loc的快 ...

随机推荐

  1. 009 使用servlet API作为参数

    1.哪些可以使用 MVC中的Handler方法可以接受ServletAPI类型的参数. 2.controller package com.spring.it; import java.io.IOExc ...

  2. C# 遍历控件 示例

    foreach(Control c in tabControl1.TabPages)//这个循环的意思是说,遍历tabControl1中所有的TabPages,TabPages是包含在tabContr ...

  3. PyPDF2详解

    工作中可能会涉及处理pdf文件,PyPDF2就是这样一个库, 使用它可以轻松的处理pdf文件,它提供了读.写.分割.合并.文件转换等多种操作.官方地址:http://mstamy2.github.io ...

  4. day01-h1字体大小和文本居中

    <!doctype html><html > <head> <meta charset="utf-8"> <link rel= ...

  5. tableview 选中一行后,不显示选中颜色

    tableview 选中一行后,不显示选中颜色 千万不要将tableview的allowsSelection设置成NO,那样的话可能导致tableview不能响应点击动作. 应该使用:cell.sel ...

  6. EF6 简单增删改查示例代码

    示例一: private DbContext _dbContext; public DbContext CurrentContext { get { if (_dbContext == null) { ...

  7. emSecure Use Digital Signatures to protect your products

    emSecure Use Digital Signatures to protect your products emSecure is an RSA based software solution ...

  8. winform 中 给DataGridView的表头添加CheckBox

    在C/S架构中,给DataGridView的表头添加CheckBox控件: 添加类:   /// <summary>       /// 给DataGridView添加全选       / ...

  9. [Python] 糗事百科文本数据的抓取

    [Python] 糗事百科文本数据的抓取 源码 https://github.com/YouXianMing/QiuShiBaiKeText import sqlite3 import time im ...

  10. SpringMVC拦截器详解

    拦截器是每个Web框架必备的功能,也是个老生常谈的主题了. 本文将分析SpringMVC的拦截器功能是如何设计的,让读者了解该功能设计的原理. 重要接口及类介绍 1. HandlerExecution ...