• pandas学习(六) STATS


    pandas学习(六) STATS

    • .to_period(‘’) 函数用于将给定的日期时间索引转换为特定频率的周期索引
    • .std() 返回每列的标准差

    1.数据集1

    在这里插入图片描述

    1.1 数据集中是否有更多男性或女性名字?

    baby_names['Gender'].value_counts()
    
    • 1

    1.2 按名称对数据集进行分组并分配到名称

    # you don't want to sum the Year column, so you delete it
    del baby_names["Year"]
    
    # group the data
    names = baby_names.groupby("Name").sum()
    
    # print the first 5 observations
    names.head()
    
    # print the size of the dataset
    print(names.shape)
    
    # sort it from the biggest value to the smallest one
    names.sort_values("Count", ascending = 0).head()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14

    在这里插入图片描述

    1.3 有多少个不同的名称的出现次数最少?

    len(names[names.Count == names.Count.min()])
    
    • 1

    1.4 名称的标准偏差是多少?

    # .std() 返回每列的标准差
    names.Count.std()
    
    • 1
    • 2

    1.5 得到数据集的 mean, min, max, std 和 quartiles.

    names.describe()
    
    • 1

    在这里插入图片描述

    2.数据集2

    数据已被修改为包含一些缺失值,由NaN标识。 使用pandas将使得这个处理更容易。

    不要使用for 循环或其他循环构造一提高效率。

    2.1 将其分配给一个名为 data 的变量,并将前 3 列替换为适当的日期时间索引。

    # parse_dates gets 0, 1, 2 columns and parses them as the index
    data_url = 'https://raw.githubusercontent.com/guipsamora/pandas_exercises/master/06_Stats/Wind_Stats/wind.data'
    data = pd.read_csv(data_url, sep = "\s+", parse_dates = [[0,1,2]]) 
    data.head()
    
    • 1
    • 2
    • 3
    • 4

    在这里插入图片描述

    2.2 2061年?我们真的有这年的数据吗?创建一个函数来修复它并应用它。

    # The problem is that the dates are 2061 and so on...
    
    # function that uses datetime
    def fix_century(x):
      year = x.year - 100 if x.year > 1989 else x.year
      return datetime.date(year, x.month, x.day)
    
    # apply the function fix_century on the column and replace the values to the right ones
    data['Yr_Mo_Dy'] = data['Yr_Mo_Dy'].apply(fix_century)
    
    # data.info()
    data.head()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12

    在这里插入图片描述

    2.3 将正确的日期设置为索引。注意数据类型,它应该是datetime64[ns]。

    # transform Yr_Mo_Dy it to date type datetime64
    data["Yr_Mo_Dy"] = pd.to_datetime(data["Yr_Mo_Dy"])
    
    # set 'Yr_Mo_Dy' as the index
    data = data.set_index('Yr_Mo_Dy')
    
    data.head()
    # data.info()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8

    2.4 计算总共有多少个未缺失值。

    #number of columns minus the number of missing values for each location
    
    data.shape[0] - data.isnull().sum()
    
    #or
    
    data.notnull().sum()
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7

    2.5 创建一个名为loc_stats的数据帧,并计算所有日期中每个位置的最小、最大和平均风速以及标准偏差

    data.describe(percentiles=[])
    
    • 1

    2.6 查找每个位置 1 月份的平均风速。

    data.loc[data.index.month == 1].mean()
    
    • 1

    2.7 将记录的采样率降低到每个位置的年度频率。

    # to_period('')描述的是该日期处于那个时期
    # 例如to_period('M') 将日期以月为单位即 xxxx-xx  某天处于某个月分
    data.groupby(data.index.to_period('A')).mean()
    
    • 1
    • 2
    • 3

    在这里插入图片描述

    2.8 将记录的采样率降低到每个位置的每月频率。

    data.groupby(data.index.to_period('M')).mean()
    
    • 1

    在这里插入图片描述

    2.9 将记录的采样率降低到每个位置的每周频率。

    data.groupby(data.index.to_period('W')).mean()
    
    • 1

    在这里插入图片描述

    2.10 计算前 52 周每周(假设第一周从 1961 年 1 月 2 日开始)所有位置的最小风速、最大风速和平均风速以及标准偏差。

    # resample data to 'W' week and use the functions
    weekly = data.resample('W').agg(['min','max','mean','std'])
    
    # slice it for the first 52 weeks and locations
    weekly.loc[weekly.index[1:53], "RPT":"MAL"] .head(10)
    
    • 1
    • 2
    • 3
    • 4
    • 5

    在这里插入图片描述

  • 相关阅读:
    华为云桌面——云端上“最卷的云”
    Java如何实现定时任务?
    不必购买Mac,这款国产设计工具能轻松替代Sketch!
    基于Java+SpringBoot+小程序实现的小说阅读管理系统
    [sinlinx-v3s]mke2fs
    Chapter8.2:非线性控制系统分析
    老油条表示真干不过,部门新来的00后测试员已把我卷崩溃,想离职了...
    提高爬虫效率之多线程、多进程的使用
    java毕业设计——基于java+mysql+socket的即时通讯软件设计与实现(毕业论文+程序源码)——即时通讯软件
    vue项目纵向撑满屏幕不出现滚动条
  • 原文地址:https://blog.csdn.net/weixin_44026026/article/details/126353723