系列文章目录
Chapter 1:创建与探索DF、排序、子集化:Python数据分析——Pandas基础入门+代码(一)
Chapter 2:聚合函数,groupby,统计分析:Python数据分析——Pandas基础入门+代码(二)
Chapter 3:索引和切片:Python数据分析——Pandas基础入门+代码(三)
Chapter 4:可视化与读写csv文件:Python数据分析——Pandas基础入门+代码(四)
Chapter 5:数据透视表:Python数据分析——Pandas基础入门+代码之数据透视表
前言
这一篇主要讲的是:如何用pandas做数据透视表 以及 时间数据上的小处理
一些非常基础的内容,把官方的文档进行了一些解释
时间数据的提取
这里希望我们做一个关于城市温度的年份数据透视表,也就是要先处理一下时间数据。
在dataframe里面,时间数据的处理是用这么一条代码
dataframe["column"].dt.[component]
这个component可以用year, month, day来代替
# 只取年份
dataframe["column"].dt.year
# 只取月份
dataframe["column"].dt.month
# 只取日期
dataframe["column"].dt.day
比如原数据是这样的
In[1]: temperatures["date"]
Out[1]:
0 2000-01-01
1 2000-02-01
2 2000-03-01
3 2000-04-01
4 2000-05-01
...
16495 2013-05-01
16496 2013-06-01
16497 2013-07-01
16498 2013