【Pandas-Cookbook】04:分组、聚集

本文通过读取CSV文件并利用Pandas库处理数据,分析了不同工作日的自行车骑行次数。通过对Berri1站点数据进行预处理,增加了表示星期几的列,并按周进行分组统计,最终绘制出每个工作日的骑行次数柱状图。
# -*-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、直方图展示
"""
评论
成就一亿技术人!
拼手气红包6.0元
还能输入1000个字符
 
红包 添加红包
表情包 插入表情
 条评论被折叠 查看
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值