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知识点汇总的更多相关文章

  1. 机器学习-Pandas 知识点汇总(吐血整理)

    Pandas是一款适用很广的数据处理的组件,如果将来从事机械学习或者数据分析方面的工作,咱们估计70%的时间都是在跟这个框架打交道.那大家可能就有疑问了,心想这个破玩意儿值得花70%的时间吗?咱不是还 ...

  2. pandas知识点脑图汇总

    参考文献: [1]Pandas知识点脑图汇总

  3. Python数据分析--Pandas知识点(三)

    本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) Python数据分析--Pandas知识点(二) 下面将是在知识点一, ...

  4. nginx几个知识点汇总

    WHY? 为什么用Nginx而不用LVS? 7点理由足以说明一切:1 .高并发连接: 官方测试能够支撑 5 万并发连接,在实际生产环境中跑到 2 - 3 万并发连接数.?2 .内存消耗少: 在 3 万 ...

  5. python全栈开发 * 10知识点汇总 * 180612

    10 函数进阶 知识点汇总 一.动态参数 形参的第三种1.动态接收位置传参 表达:*args (在参数位置编写 * 表⽰接收任意内容) (1)动态位置参数def eat(*args): print(a ...

  6. Python数据分析--Pandas知识点(二)

    本文主要是总结学习pandas过程中用到的函数和方法, 在此记录, 防止遗忘. Python数据分析--Pandas知识点(一) 下面将是在知识点一的基础上继续总结. 13. 简单计算 新建一个数据表 ...

  7. 清华大学OS操作系统实验lab1练习知识点汇总

    lab1知识点汇总 还是有很多问题,但是我觉得我需要在查看更多资料后回来再理解,学这个也学了一周了,看了大量的资料...还是它们自己的80386手册和lab的指导手册觉得最准确,现在我就把这部分知识做 ...

  8. c++ 函数知识点汇总

    c++ 函数知识点汇总 swap函数 交换两个数组元素 比如 swap(a[i],a[j]); 就是交换a[i] 和 a[j] 的值 strcpy() 复制一个数组元素的值到另一个数组元素里 strc ...

  9. 前端开发 JavaScript 干货知识点汇总

    很多初学的朋友经常问我,前端JavaScript都需要学习哪些东西呀?哪些是JavaScript的重点知识啊? 其实做前端开发工程师,所有的知识点都是我们学习必备的东西,只有扎实的技术基础才是高薪的关 ...

随机推荐

  1. 【最大流Dinic模板】HDU1532&POJ1273-Drainage Ditches(16/3/6更正)

    #include<iostream> #include<cstdio> #include<cstdlib> #include<cstring> #inc ...

  2. 《SQL Server企业级平台管理实践》读书笔记

    http://www.cnblogs.com/zhijianliutang/category/277162.html

  3. Windows删除文件时出现,“正在准备 再循环”

    初步分析这个问题是由于文件写入时,由于意外事情的发生(如,文件太大,正在写入时你取消了,而系统又没有来得及删除没有写完的数据等原因)没有写完. 错误的类型如下 出现此问题.当你删除不成时.返回系统根目 ...

  4. mysql 中添加索引的三种方法

    原文:http://www.andyqian.com/2016/04/06/database/mysqleindex/ 在mysql中有多种索引,有普通索引,全文索引,唯一索引,多列索引,小伙伴们可以 ...

  5. 【spring】【spring mvc】【spring boot】获取spring cloud项目中所有spring mvc的请求资源

    实现的方法: 1.在父级项目中 或者 每个微服务都引用的项目中添加实体类Resource 2.在父级项目中 或者 每个为服务都引用的项目中写一个工具类,作用是用来获取请求资源 3.在每一个微服务的启动 ...

  6. Quartz_TimeJob例子(C#)

    执行入口: using System; using System.Collections.Generic; using log4net; using Quartz; using ypt_base.Co ...

  7. iOS面试题合集(77道)

    1.#import和#include的区别 @class? @class一般用于头文件中需要声明该类的某个实例变量的时候用到,在m文 件中还是需要使用#import 而#import比起#includ ...

  8. requests.exceptions.SSLError: hostname '127.0.0.1' doesn't match None

    http://stackoverflow.com/questions/33429453/python-requests-ssl-hostname-doesnt-match-error http://w ...

  9. hibernate反向工程 (eclipse和myeclipse)【转】

    myeclipse下hibernate反向工程: 1.选择myeclipse hibernate视图 2.建立与后台数据库的连接 1).configure database driver: 2).添加 ...

  10. [转]Oracle connection strings

    本文转自:http://www.connectionstrings.com/oracle/ Standard Data Source=MyOracleDB;Integrated Security=ye ...