【Pandas-Cookbook】04:分组、聚集
2017-02-16 15:51
155 查看
# -*-coding:utf-8-*- # by kevinelstri # 2017.2.16 # --------------------- # Chapter 4: Find out on which weekday people bike the most with groupby and aggregate # --------------------- import pandas as pd import matplotlib.pyplot as plt """ 4.1 Adding a 'weekday' column to our dataframe """ bikes = pd.read_csv('../data/bikes.csv', sep=';', encoding='latin1', index_col='Date', parse_dates=['Date'], dayfirst=True) print bikes.head() bikes['Berri 1'].plot() # 绘制曲线 # plt.show() berri_bikes = bikes[['Berri 1']].copy() # 将某一列的数据复制出来,单独为一列 print berri_bikes[:5] print berri_bikes.index print berri_bikes.index.day print berri_bikes.index.weekday berri_bikes.loc[:, 'weekday'] = berri_bikes.index.weekday print berri_bikes[:5] """ 4.2 Adding up the cyclists by weekday """ """ 使用DataFrames中的.groupby()方法进行分组,并计算每一组的数量和 """ weekday_counts = berri_bikes.groupby('weekday').aggregate(sum) print weekday_counts weekday_counts.index = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] print weekday_counts weekday_counts.plot(kind='bar') # plt.show() """ 4.3 Putting it together """ """ 所有代码汇总 """ bikes = pd.read_csv('../data/bikes.csv', sep=';', encoding='latin1', index_col='Date', dayfirst=True, parse_dates=['Date']) berri_bikes = bikes[['Berri 1']].copy() berri_bikes.loc[:, 'weekday'] = berri_bikes.index.weekday weekday_counts = berri_bikes.groupby('weekday').aggregate(sum) weekday_counts.index = ['Monday', 'Tuesday', 'Wednesday', 'Thursday', 'Friday', 'Saturday', 'Sunday'] weekday_counts.plot(kind='bar') plt.show() """ 分析: 主要是计算时间,分组处理一周时间,将每周对应的数量加到对应的天上 方法: 1、csv数据的读取 2、列数据的复制 3、将数据按照一周来进行划分 4、按照一周进行分组处理数据,修改索引 5、直方图展示 """
相关文章推荐
- python科学计算笔记(六)pandas 分组groupby
- pandas官方文档cookbook(6)中Split&Pivot&Apply翻译
- 利用python进行数据分析-pandas.concat/subplots/gropuby/pivot_table,多文件整合、聚合、分组,子图
- 【Pandas-Cookbook】02:数据选择
- 【Pandas-Cookbook】09:加载SQL数据库数据
- pandas 按照列A分组,将同一组的列B求和,生成新的Dataframe
- 精通数据库SQL——聚集分析与分组
- 【Pandas-Cookbook】03:噪音数据处理
- 【Pandas-Cookbook】08:时间戳处理
- pandas Cookbook--Chapter 1
- pandas—数据聚合与分组运算
- Pandas分组运算后数据的合并
- 【Pandas-Cookbook】05:DataFrame框架案例
- 分组聚集的K-means算法
- pandas中的groupby函数的分组结果怎么保存成DataFrame
- pandas 按照列A分组,将同一组的列B求和,生成新的Dataframe
- pandas dataframe 分组聚合时,分组组名并入列的方法
- [04]分组元素
- python/pandas数据挖掘(十四)-groupby,聚合,分组级运算
- pandas中的groupby函数的分组结果怎么保存成DataFrame