# Author:Zhang Yuan
import pandas as pd
import numpy as np #Pandas提供了两大数据结构:一维结构的Series类型、二维结构的DataFrame类型。 #Series对象本质上是Numpy对象,具有index和values两大属性。 #对于输入的valuas,Series会默认位置索引0、1、2、3...,还可以自定义标签索引。 #Series切片支持“标签切片”和“位置切片”。位置切片即Python切片,包括头不包括尾;但“标签切片”包括头包括尾。之所以这样设计是因为,通常我们不知道标签的顺序,无法知道末尾标签下一个标签是什么。 #时间序列Series,在索引和切片方面有优化:
from datetime import datetime
dates=[datetime(2016,1,1),datetime(2016,1,2),datetime(2016,1,3),datetime(2016,2,1)]
ts=pd.Series([1,2,3,4],index=dates)
print(ts[""],ts["2016-01-01"],ts["01/01/2016"]) #时间序列多种字符串索引
print(ts[""]) #只传入年或年月来切片
print(ts["2016-01"]) #只传入年或年月来切片
print(ts["2016-01":"2016-03"]) #切片字符串时间戳可以不必存于index中 #DataFrame是一个表格型的数据结构,每一列代表一个变量,每一行是一条记录。简单来说,DataFrame是共享同一个index的Series的集合。 #DataFrame对象的索引和切片---------------------------------------------------
dates=[datetime(2016,1,i) for i in range(1,10)]
df=pd.DataFrame(np.random.randn(9,4),index=dates,columns=list("ABCD"))
print(df[0:3]) #对行切片
print(df["A"]) #提取单独一列
print(df[["A","C"]]) #提取多列
print(df[df["A"]>0]) #根据boolean值提取行
#PS注意:对列直接切片出错:df["A":"C"];直接同时的操作行列也出错:df[1:3,"A"]
#如果要行列操作,需要用方法:标签索引和切片loc[]
print(df.loc[:,"A"]) #提取一列
print(df.loc[:,"A":"C"]) #列切片
print(df.loc[dates[0:4],"A":"C"]) #行列切片
print(df.loc[dates[0],"A"]) #特定值
print(df.loc[df.loc[:,"A"]>0]) #根据boolean值提取
#如果要行列操作,需要用方法:位置索引和切片iloc[]
print(df.iloc[2]) #提取行,相当于df.iloc[2,:]
print(df.iloc[:,2]) #提取列
print(df.iloc[[1,4],[2,3]]) #提取多个行列值,不是切片,类似numpy
print(df.iloc[1:5,2:4]) #切片
print(df.iloc[2,3]) #提取特定值
#----------------------------------------------------------------------------- #Series与DataFrame对象的运算
#Series与Series是index匹配运算
s1=pd.Series([1,2,3],index=list("ABC"))
s2=pd.Series([4,5,6],index=list("BCD"))
s2-s1
#DataFrame与Series是DataFrame的column与Series的index匹配,PS:不是index匹配
df1=pd.DataFrame(np.arange(1,13).reshape(3,4),index=list("abc"),columns=list("ABCD"))
df1-s1
#DataFrame与DataFrame是同时对index与column匹配
df2=pd.DataFrame(np.arange(1,13).reshape(4,3),index=list("bcde"),columns=list("CDE"))
df1*df2 #DataFrame的轴axis与numpy一样。0轴-Y轴-列数据、1轴-X轴-行数据。

最新文章

  1. Linux下的C编程实战
  2. 【代码笔记】iOS-切换条
  3. 【POJ 2096】Collecting Bugs 概率期望dp
  4. paper 84:机器学习算法--随机森林
  5. TypeError: The CanvasRenderingContext2D.webkitBackingStorePixelRatio getter can only be used on instances of CanvasRenderingContext2D
  6. C# 如何将字符串形式的” \\u1234 “ 为 “ \u1234” 的unicode编码解码为中文
  7. CPU 时间片 分时 轮转调度
  8. heritrix
  9. Vmware 8.00 文件共享ubuntu
  10. PHP常用字符串函数
  11. sctf pwn400
  12. Java中的流程控制(二)
  13. PHP新手之学习类与对象(2)
  14. AngularJS1.X学习笔记10-自定义指令(下)
  15. 非正则表达式检验邮箱格式是否合法(Java代码实现)
  16. ASP.NET Core 2.2 : 十七.Action的执行(Endpoint.RequestDelegate后面的故事)
  17. Linux图形化监控网络流量:speedometer查看流量
  18. XML文档中的xmlns、xmlns:xsi和xsi:schemaLocation
  19. Matlab产生波形数据文件
  20. [LeetCode&Python] Problem 21. Merge Two Sorted Lists

热门文章

  1. mvn从下载安装到纯命令行创建第一个mvn程序(编码,编译,测试,安装,打包)全过程细致分解
  2. POJ-3275:Ranking the Cows(Floyd、bitset)
  3. 2017"百度之星"程序设计大赛 - 初赛(A)今夕何夕
  4. HTML <form> 标签的 enctype
  5. .Net魔兽登录页面
  6. Java GUI 事件监听
  7. mongodb 上限集合
  8. {g2o}Installation Notes:ccmake
  9. LR中订单流程脚本
  10. PG extract 函数示例