pandas知识点汇总
pandas基础知识汇总
1.时间序列
import pandas as pd
import numpy as np
import matplotlib.pyplot as plt
from datetime import datetime
now=datetime.now()
now
datetime.datetime(2018, 11, 18, 16, 44, 4, 405600)
print(now.strftime('%Y-%m-%d'))
print(datetime.strptime('7/6/2018','%m/%d/%Y'))
print(now.strftime('%X'))
2018-11-18
2018-07-06 00:00:00
16:44:04
dates=pd.date_range('11/1/2018',periods=50,freq='W-WED')
long_df=pd.DataFrame(np.random.randn(50,4),index=dates,columns=list('ABCD'))
long_df.head(10)
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
A | B | C | D | |
---|---|---|---|---|
2018-11-07 | 0.215536 | 0.855986 | 0.737170 | -0.440150 |
2018-11-14 | -0.477099 | 0.467430 | -0.107105 | 0.941922 |
2018-11-21 | 0.052926 | -0.671084 | 0.219058 | -0.350776 |
2018-11-28 | -1.449668 | 0.003958 | 1.065875 | -0.277673 |
2018-12-05 | 1.371631 | 0.542839 | 0.071466 | 0.609508 |
2018-12-12 | 0.322176 | 1.335534 | -0.423240 | -0.111549 |
2018-12-19 | -0.564089 | 0.262918 | 0.477552 | 0.018652 |
2018-12-26 | -0.490212 | 0.382492 | -0.858712 | -0.920786 |
2019-01-02 | 1.630409 | -0.740542 | 1.296362 | 0.376437 |
2019-01-09 | 1.460070 | -0.449293 | -0.783725 | -1.098911 |
resample=long_df.resample('M').mean()
resample
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
A | B | C | D | |
---|---|---|---|---|
2018-11-30 | -0.414576 | 0.164073 | 0.478750 | -0.031669 |
2018-12-31 | 0.159876 | 0.630946 | -0.183234 | -0.101044 |
2019-01-31 | 0.092189 | -0.225606 | 0.251072 | -0.456075 |
2019-02-28 | -0.124615 | -0.467522 | -0.142258 | 0.195602 |
2019-03-31 | -0.294693 | -0.014264 | 0.725285 | 1.291576 |
2019-04-30 | 0.182648 | 0.231022 | -0.458572 | 0.294329 |
2019-05-31 | 0.317648 | 0.060677 | 0.297406 | -0.035691 |
2019-06-30 | 0.407404 | -0.198072 | -0.461785 | 1.074969 |
2019-07-31 | -0.245908 | 0.150161 | 0.526564 | -0.082258 |
2019-08-31 | 0.046819 | -0.227364 | -0.684359 | 0.033979 |
2019-09-30 | -0.834454 | 1.186670 | 0.653583 | -0.306585 |
2019-10-31 | -0.436990 | -0.460347 | 0.040175 | 0.681903 |
pd.date_range('11/18/2018',periods=10,freq='2h30min')
DatetimeIndex(['2018-11-18 00:00:00', '2018-11-18 02:30:00',
'2018-11-18 05:00:00', '2018-11-18 07:30:00',
'2018-11-18 10:00:00', '2018-11-18 12:30:00',
'2018-11-18 15:00:00', '2018-11-18 17:30:00',
'2018-11-18 20:00:00', '2018-11-18 22:30:00'],
dtype='datetime64[ns]', freq='150T')
type(resample)
pandas.core.resample.DatetimeIndexResampler
ts=pd.Series(np.arange(10),index=pd.date_range('11/18/2018',periods=10,freq='T'))
ts
2018-11-18 00:00:00 0
2018-11-18 00:01:00 1
2018-11-18 00:02:00 2
2018-11-18 00:03:00 3
2018-11-18 00:04:00 4
2018-11-18 00:05:00 5
2018-11-18 00:06:00 6
2018-11-18 00:07:00 7
2018-11-18 00:08:00 8
2018-11-18 00:09:00 9
Freq: T, dtype: int32
#pay attention to the parameter 'closed'
ts.resample('3min',closed='left',label='left').sum()
2018-11-18 00:00:00 3
2018-11-18 00:03:00 12
2018-11-18 00:06:00 21
2018-11-18 00:09:00 9
Freq: 3T, dtype: int32
ts.resample('3min').ohlc()
.dataframe tbody tr th:only-of-type {
vertical-align: middle;
}
.dataframe tbody tr th {
vertical-align: top;
}
.dataframe thead th {
text-align: right;
}
open | high | low | close | |
---|---|---|---|---|
2018-11-18 00:00:00 | 0 | 2 | 0 | 2 |
2018-11-18 00:03:00 | 3 | 5 | 3 | 5 |
2018-11-18 00:06:00 | 6 | 8 | 6 | 8 |
2018-11-18 00:09:00 | 9 | 9 | 9 | 9 |
long_df.plot()
## 滑窗函数
fig,axes=plt.subplots(1,3,figsize=(20,4))
long_df['A'].plot(ax=axes[0])
long_df['A'].rolling(window=10).mean().plot(ax=axes[0],title='A_10_mean')
long_df['B'].plot(ax=axes[1])
long_df['B'].rolling(window=10).sum().plot(ax=axes[1],title='B_10_sum')
long_df['C'].plot(ax=axes[2])
long_df['C'].rolling(window=10).quantile(quantile=0.8).plot(ax=axes[2],title='C_10_quantile')
#corr
from pylab import mpl
mpl.rcParams['font.sans-serif'] = ['SimHei'] # 指定默认字体
mpl.rcParams['axes.unicode_minus'] = False # 解决保存图像是负号'-'显示为方块的问题
long_df['B'].rolling(window=10).corr(long_df['A']).plot(style='ro--',grid=True,title='二元函数相关系数')
2.matplotlib绘图
long_df['A'].plot(kind='kde',style='g')
pd.plotting.scatter_matrix(long_df,diagonal='kde',color='r')
df=pd.DataFrame(np.random.randn(6,4),index='one two three four five six'.split(' '),columns=list('ABCD'))
df_normal=abs(df).div(abs(df).sum(1),axis=0)
df_normal.plot(kind='barh',stacked=True)
abs(df).sum(1)
one 3.989060
two 1.160160
three 2.087209
four 2.680116
five 4.452365
six 2.298789
dtype: float64
pandas知识点汇总的更多相关文章
- 机器学习-Pandas 知识点汇总(吐血整理)
Pandas是一款适用很广的数据处理的组件,如果将来从事机械学习或者数据分析方面的工作,咱们估计70%的时间都是在跟这个框架打交道.那大家可能就有疑问了,心想这个破玩意儿值得花70%的时间吗?咱不是还 ...
- pandas知识点脑图汇总
参考文献: [1]Pandas知识点脑图汇总
- Python数据分析--Pandas知识点(三)
本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) Python数据分析--Pandas知识点(二) 下面将是在知识点一, ...
- nginx几个知识点汇总
WHY? 为什么用Nginx而不用LVS? 7点理由足以说明一切:1 .高并发连接: 官方测试能够支撑 5 万并发连接,在实际生产环境中跑到 2 - 3 万并发连接数.?2 .内存消耗少: 在 3 万 ...
- python全栈开发 * 10知识点汇总 * 180612
10 函数进阶 知识点汇总 一.动态参数 形参的第三种1.动态接收位置传参 表达:*args (在参数位置编写 * 表⽰接收任意内容) (1)动态位置参数def eat(*args): print(a ...
- Python数据分析--Pandas知识点(二)
本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) 下面将是在知识点一的基础上继续总结. 13. 简单计算 新建一个数据表 ...
- 清华大学OS操作系统实验lab1练习知识点汇总
lab1知识点汇总 还是有很多问题,但是我觉得我需要在查看更多资料后回来再理解,学这个也学了一周了,看了大量的资料...还是它们自己的80386手册和lab的指导手册觉得最准确,现在我就把这部分知识做 ...
- c++ 函数知识点汇总
c++ 函数知识点汇总 swap函数 交换两个数组元素 比如 swap(a[i],a[j]); 就是交换a[i] 和 a[j] 的值 strcpy() 复制一个数组元素的值到另一个数组元素里 strc ...
- 前端开发 JavaScript 干货知识点汇总
很多初学的朋友经常问我,前端JavaScript都需要学习哪些东西呀?哪些是JavaScript的重点知识啊? 其实做前端开发工程师,所有的知识点都是我们学习必备的东西,只有扎实的技术基础才是高薪的关 ...
随机推荐
- python基础之序列化 time random os
序列化与反序列化 json pickle 1.什么是序列化与反序列化? 序列化就是将内存中的数据结构转成一种中间格式储存到硬盘或者基于网络传输 反序列化是网络,硬盘将被序列化的对象重新读到内存 2. ...
- (原创)Stanford Machine Learning (by Andrew NG) --- (week 5) Neural Networks Learning
本栏目内容来自Andrew NG老师的公开课:https://class.coursera.org/ml/class/index 一般而言, 人工神经网络与经典计算方法相比并非优越, 只有当常规方法解 ...
- Java并发(六):volatile的实现原理
synchronized是一个重量级的锁,volatile通常被比喻成轻量级的synchronized volatile是一个变量修饰符,只能用来修饰变量. volatile写:当写一个volatil ...
- Problem B: 输入3个字符串,按由小到大顺序输出
#include<stdio.h> #include<string.h> int main() { ],b[],c[],t[]; while(gets(a)!=NULL) { ...
- ES6 Set结构和Map结构(上)
Set ES6提供了新的数据结构--Set,它类似于数组,但是成员的值都是唯一的,没有重复的值. Set本身也是一个构造函数,用来生成Set数据结构 var s = new Set(); [2,3,5 ...
- 客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值。
客户端的javascript改变了asp.net webform页面控件的值,后台代码中如何获取修改后的值. 无论是什么的html控件,只要加上了runat="server" ...
- 【Todo】Boost安装与学习
现在这里找下载包 http://sourceforge.net/projects/boost 我找的是 1_62_0 下面是从公司wiki上找到的一个说明. boost & thrift安装步 ...
- Walle代码发布系统
Walle 一个web部署系统工具,配置简单.功能完善.界面流畅.开箱即用!支持git.svn版本管理,支持各种web代码发布,PHP,Python,JAVA等代码的发布.回滚,可以通过web来一键完 ...
- 用sencha touch的Cmd创建的MVC工程需要注意的问题
用ST的cmd创建的js文件都是ANSI编码格式的,所以导致无法正常显示中文.例如传输的参数为中文时就为乱码,导致各种问题... 解决办法:将js文件用记事本打开,另存为,选择编码为UTF-8,覆盖原 ...
- 【笔记】git 的常用操作命令(持续更新。。。)
项目正在如火如荼的开展,代码量的繁多不得不令我们运用 git 这个有用的工具去管理我们共同协作的代码 git 在这里不作什么介绍了,百度一大堆的教程 首推廖雪峰老师的:http://www.liaox ...