《Pandas Cookbook》第03章 数据分析入门

简介: 第01章 Pandas基础第02章 DataFrame运算第03章 数据分析入门第04章 选取数据子集第05章 布尔索引第06章 索引对齐第07章 分组聚合、过滤、转换第08章 数据清理第09章 合并Pandas对象第10章 时间序列分析第1...

第01章 Pandas基础
第02章 DataFrame运算
第03章 数据分析入门
第04章 选取数据子集
第05章 布尔索引
第06章 索引对齐
第07章 分组聚合、过滤、转换
第08章 数据清理
第09章 合并Pandas对象
第10章 时间序列分析
第11章 用Matplotlib、Pandas、Seaborn进行可视化


In[1]: import pandas as pd
       import numpy as np
       from IPython.display import display
       pd.options.display.max_columns = 50

1. 规划数据分析路线

# 读取查看数据
 In[2]: college = pd.read_csv('data/college.csv')
 In[3]: college.head()
Out[3]: 
img_ab3296ea4fd3f190d693095688d1eca4.png
# 数据的行数与列数
 In[4]: college.shape
Out[4]: (7535, 27)
# 统计数值列,并进行转置
 In[5]: with pd.option_context('display.max_rows', 8):
    display(college.describe(include=[np.number]).T)
Out[5]: 
img_58c46101fa16fc294194972c82c235bb.png
# 统计对象和类型列
 In[6]: college.describe(include=[np.object, pd.Categorical]).T
Out[6]: 
img_9e66372a41851df6abab120528673a31.png
# 列出每列的数据类型,非缺失值的数量,以及内存的使用
 In[7]: college.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7535 entries, 0 to 7534
Data columns (total 27 columns):
INSTNM                7535 non-null object
CITY                  7535 non-null object
STABBR                7535 non-null object
HBCU                  7164 non-null float64
MENONLY               7164 non-null float64
WOMENONLY             7164 non-null float64
RELAFFIL              7535 non-null int64
SATVRMID              1185 non-null float64
SATMTMID              1196 non-null float64
DISTANCEONLY          7164 non-null float64
UGDS                  6874 non-null float64
UGDS_WHITE            6874 non-null float64
UGDS_BLACK            6874 non-null float64
UGDS_HISP             6874 non-null float64
UGDS_ASIAN            6874 non-null float64
UGDS_AIAN             6874 non-null float64
UGDS_NHPI             6874 non-null float64
UGDS_2MOR             6874 non-null float64
UGDS_NRA              6874 non-null float64
UGDS_UNKN             6874 non-null float64
PPTUG_EF              6853 non-null float64
CURROPER              7535 non-null int64
PCTPELL               6849 non-null float64
PCTFLOAN              6849 non-null float64
UG25ABV               6718 non-null float64
MD_EARN_WNE_P10       6413 non-null object
GRAD_DEBT_MDN_SUPP    7503 non-null object
dtypes: float64(20), int64(2), object(5)
memory usage: 1.6+ MB
# 重复了,但没设置最大行数
 In[8]: college.describe(include=[np.number]).T
Out[8]: 
img_776cb7a854cace3e41579ae25cc0e647.png
# 和前面重复了
 In[9]: college.describe(include=[np.object, pd.Categorical]).T
Out[9]: 
img_142a3fddaedd5b90e083c3699b2dd76e.png

更多

# 在describe方法中,打印分位数
 In[10]: with pd.option_context('display.max_rows', 5):
    display(college.describe(include=[np.number], 
                 percentiles=[.01, .05, .10, .25, .5, .75, .9, .95, .99]).T)
img_bf1941feef7a12789a35a1b8bd888cd4.png
# 展示一个数据字典:数据字典的主要作用是解释列名的意义
 In[11]: college_dd = pd.read_csv('data/college_data_dictionary.csv')
 In[12]: with pd.option_context('display.max_rows', 8):
             display(college_dd)
img_273c36aef56a12e3f353956dbf6477be.png

2. 改变数据类型,降低内存消耗

# 选取五列
 In[13]: college = pd.read_csv('data/college.csv')
         different_cols = ['RELAFFIL', 'SATMTMID', 'CURROPER', 'INSTNM', 'STABBR']
         col2 = college.loc[:, different_cols]
         col2.head()
Out[13]:
img_3a3ff616a5453a212620dedb69b8328e.png
# 查看数据类型
 In[14]: col2.dtypes
Out[14]: RELAFFIL      int64
         SATMTMID    float64
         CURROPER      int64
         INSTNM       object
         STABBR       object
         dtype: object
# 用memory_usage方法查看每列的内存消耗
 In[15]: original_mem = col2.memory_usage(deep=True)
         original_mem
Out[15]: Index           80
         RELAFFIL     60280
         SATMTMID     60280
         CURROPER     60280
         INSTNM      660240
         STABBR      444565
         dtype: int64
# RELAFFIL这列只包含0或1,因此没必要用64位,使用astype方法将其变为8位(1字节)整数
 In[16]: col2['RELAFFIL'] = col2['RELAFFIL'].astype(np.int8)
# 再次查看数据类型
 In[17]: col2.dtypes
Out[17]: RELAFFIL       int8
         SATMTMID    float64
         CURROPER      int64
         INSTNM       object
         STABBR       object
         dtype: object
# 检查两个对象列的独立值的个数
 In[18]: col2.select_dtypes(include=['object']).nunique()
Out[18]: INSTNM    7535
         STABBR      59
         dtype: int64
# STABBR列可以转变为“类型”(Categorical),独立值的个数小于总数的1%
 In[19]: col2['STABBR'] = col2['STABBR'].astype('category')
         col2.dtypes
Out[19]: RELAFFIL        int8
         SATMTMID     float64
         CURROPER       int64
         INSTNM        object
         STABBR      category
         dtype: object
# 再次检查内存的使用
 In[20]: new_mem = col2.memory_usage(deep=True)
         new_mem
Out[20]: Index           80
         RELAFFIL      7535
         SATMTMID     60280
         CURROPER     60280
         INSTNM      660699
         STABBR       13576
         dtype: int64
# 通过和原始数据比较,RELAFFIL列变为了原来的八分之一,STABBR列只有原始大小的3%
 In[21]: new_mem / original_mem
Out[21]: Index       1.000000
         RELAFFIL    0.125000
         SATMTMID    1.000000
         CURROPER    1.000000
         INSTNM      1.000695
         STABBR      0.030538
         dtype: float64

更多

# CURROPER和INSTNM分别是int64和对象类型
 In[22]: college = pd.read_csv('data/college.csv')
 In[23]: college[['CURROPER', 'INSTNM']].memory_usage(deep=True)
Out[23]: Index           80
         CURROPER     60280
         INSTNM      660240
         dtype: int64
# CURROPER列加上了10000000,但是内存使用没有变化;但是INSTNM列加上了一个a,内存消耗增加了105字节
 In[24]: college.loc[0, 'CURROPER'] = 10000000
         college.loc[0, 'INSTNM'] = college.loc[0, 'INSTNM'] + 'a'
         # college.loc[1, 'INSTNM'] = college.loc[1, 'INSTNM'] + 'a'
         college[['CURROPER', 'INSTNM']].memory_usage(deep=True)
Out[24]: Index           80
         CURROPER     60280
         INSTNM      660345
         dtype: int64
# 数据字典中的信息显示MENONLY这列只包含0和1,但是由于含有缺失值,它的类型是浮点型
 In[25]: college['MENONLY'].dtype
Out[25]: dtype('float64')
# 任何数值类型的列,只要有一个缺失值,就会成为浮点型;这列中的任何整数都会强制成为浮点型
 In[26]: college['MENONLY'].astype('int8') # ValueError: Cannot convert non-finite values (NA or inf) to integer
---------------------------------------------------------------------------
ValueError                                Traceback (most recent call last)
<ipython-input-26-98afc27c1701> in <module>()
----> 1 college['MENONLY'].astype('int8') # ValueError: Cannot convert non-finite values (NA or inf) to integer

~/anaconda3/lib/python3.6/site-packages/pandas/util/_decorators.py in wrapper(*args, **kwargs)
    116                 else:
    117                     kwargs[new_arg_name] = new_arg_value
--> 118             return func(*args, **kwargs)
    119         return wrapper
    120     return _deprecate_kwarg

~/anaconda3/lib/python3.6/site-packages/pandas/core/generic.py in astype(self, dtype, copy, errors, **kwargs)
   4002         # else, only a single dtype is given
   4003         new_data = self._data.astype(dtype=dtype, copy=copy, errors=errors,
-> 4004                                      **kwargs)
   4005         return self._constructor(new_data).__finalize__(self)
   4006 

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in astype(self, dtype, **kwargs)
   3455 
   3456     def astype(self, dtype, **kwargs):
-> 3457         return self.apply('astype', dtype=dtype, **kwargs)
   3458 
   3459     def convert(self, **kwargs):

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in apply(self, f, axes, filter, do_integrity_check, consolidate, **kwargs)
   3322 
   3323             kwargs['mgr'] = self
-> 3324             applied = getattr(b, f)(**kwargs)
   3325             result_blocks = _extend_blocks(applied, result_blocks)
   3326 

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in astype(self, dtype, copy, errors, values, **kwargs)
    542     def astype(self, dtype, copy=False, errors='raise', values=None, **kwargs):
    543         return self._astype(dtype, copy=copy, errors=errors, values=values,
--> 544                             **kwargs)
    545 
    546     def _astype(self, dtype, copy=False, errors='raise', values=None,

~/anaconda3/lib/python3.6/site-packages/pandas/core/internals.py in _astype(self, dtype, copy, errors, values, klass, mgr, **kwargs)
    623 
    624                 # _astype_nansafe works fine with 1-d only
--> 625                 values = astype_nansafe(values.ravel(), dtype, copy=True)
    626                 values = values.reshape(self.shape)
    627 

~/anaconda3/lib/python3.6/site-packages/pandas/core/dtypes/cast.py in astype_nansafe(arr, dtype, copy)
    685 
    686         if not np.isfinite(arr).all():
--> 687             raise ValueError('Cannot convert non-finite values (NA or inf) to '
    688                              'integer')
    689 

ValueError: Cannot convert non-finite values (NA or inf) to integer
# 对于数据类型,可以替换字符串名:27、28、30、31是等价的
 In[27]: college.describe(include=['int64', 'float64']).T
Out[27]: 
img_55012b774d901893341c0293bd9161f1.png
 In[28]: college.describe(include=[np.int64, np.float64]).T
Out[28]: 
img_9c8e307e4c4baf57d0ffaad5a933e1b1.png
 In[29]: college['RELAFFIL'] = college['RELAFFIL'].astype(np.int8)
 In[30]: college.describe(include=['int', 'float']).T  # defaults to 64 bit int/floats
Out[30]: 
img_f846baa4e7018cb8c58cb87ede874c98.png
 In[31]: college.describe(include=['number']).T  # also works as the default int/float are 64 bits
Out[31]: 
img_75030c205f80b4ac8f642cdf2ea26e1a.png
# 转变数据类型时也可以如法炮制
 In[32]: college['MENONLY'] = college['MENONLY'].astype('float16')
         college['RELAFFIL'] = college['RELAFFIL'].astype('int8')
 In[33]: college.index = pd.Int64Index(college.index)
         college.index.memory_usage()
Out[33]: 60280

3. 从最大中选择最小

# 读取movie.csv,选取'movie_title', 'imdb_score', 'budget'三列
 In[34]: movie = pd.read_csv('data/movie.csv')
         movie2 = movie[['movie_title', 'imdb_score', 'budget']]
         movie2.head()
Out[34]: 
img_e458c10535b1719928f5719655970bde.png
# 用nlargest方法,选出imdb_score分数最高的100个
 In[35]: movie2.nlargest(100, 'imdb_score').head()
Out[35]: 
img_fb0362cbafc10a6b335a2f7826e20044.png
# 用链式操作,nsmallest方法再从中挑出预算最小的五部
 In[36]: movie2.nlargest(100, 'imdb_score').nsmallest(5, 'budget')
Out[36]: 
img_399636c5ef4ac26eb725e1de6b801480.png

4. 通过排序选取每组的最大值

# 同上,选取出三列。按照title_year降序排列
 In[37]: movie = pd.read_csv('data/movie.csv')
         movie2 = movie[['movie_title', 'title_year', 'imdb_score']]
 In[38]: movie2.sort_values('title_year', ascending=False).head()
Out[38]: 
img_b3c4383f267488e8043f2633c7c29bf9.png
# 用列表同时对两列进行排序
 In[39]: movie3 = movie2.sort_values(['title_year','imdb_score'], ascending=False)
         movie3.head()
Out[39]: 
img_02610eb15db2d84a754276667986a521.png
# 用drop_duplicates去重,只保留每年的第一条数据
 In[40]: movie_top_year = movie3.drop_duplicates(subset='title_year')
         movie_top_year.head()
Out[40]: 
img_fa47abc75bcc9d1931a5f6e9da4fb6e2.png
# 通过给ascending设置列表,可以同时对一列降序排列,一列升序排列
 In[41]: movie4 = movie[['movie_title', 'title_year', 'content_rating', 'budget']]
         movie4_sorted = movie4.sort_values(['title_year', 'content_rating', 'budget'], 
                                   ascending=[False, False, True])
         movie4_sorted.drop_duplicates(subset=['title_year', 'content_rating']).head(10)
Out[41]: 
img_eefc5adff30775a3f684e393e751d92c.png

5. 用sort_values复现nlargest方法

# 和前面一样nlargest和nsmallest链式操作进行选取
 In[42]: movie = pd.read_csv('data/movie.csv')
         movie2 = movie[['movie_title', 'imdb_score', 'budget']]
         movie_smallest_largest = movie2.nlargest(100, 'imdb_score').nsmallest(5, 'budget')
         movie_smallest_largest
Out[42]: 
img_76c2f991aa33be963a795792706a75ac.png
# 用sort_values方法,选取imdb_score最高的100个
 In[43]: movie2.sort_values('imdb_score', ascending=False).head(100).head()
Out[43]: 
# 然后可以再.sort_values('budget').head(),选出预算最低的5个,结果如下
img_eda63ebadc9417a27353de23a134ee43.png

这两种方法得到的最小的5部电影不同,用tail进行调查:

# tail可以查看尾部
 In[45]: movie2.nlargest(100, 'imdb_score').tail()
Out[45]: 
img_eda63ebadc9417a27353de23a134ee43.png
 In[46]: movie2.sort_values('imdb_score', ascending=False).head(100).tail()
Out[46]: 
img_096deee1ddc1267f6758a901f7aa9888.png

这是因为评分在8.4以上的电影超过了100部。

6. 计算跟踪止损单价格

# pip install pandas_datareader 或 conda install pandas_datareader,来安装pandas_datareader
 In[47]: import pandas_datareader as pdr

笔记:pandas_datareader的问题
pandas_datareader在读取“google”源时会有问题。如果碰到问题,切换到“Yahoo”。

# 查询特斯拉在2017年第一天的股价
 In[49]: tsla = pdr.DataReader('tsla', data_source='yahoo',start='2017-1-1')
         tsla.head(8)
Out[49]: 
img_9a23098482b17528365b92746ec7d7fa.png
# 只关注每天的收盘价,使用cummax得到迄今为止的收盘价最大值
 In[50]: tsla_close = tsla['Close']
 In[51]: tsla_cummax = tsla_close.cummax()
         tsla_cummax.head(8)
Out[51]: 
img_cba9f7e0dcd355c4078d47bec5edb6f0.png
# 将下行区间限制到10%,将tsla_cummax乘以0.9
>>> tsla_trailing_stop = tsla_cummax * .9
>>> tsla_trailing_stop.head(8)
Date
2017-01-03    195.291
2017-01-04    204.291
2017-01-05    204.291
2017-01-06    206.109
2017-01-09    208.152
2017-01-10    208.152
2017-01-11    208.152
2017-01-12    208.152
Name: Close, dtype: float64

更多

# 将上述功能包装成一个函数
 In[52]: def set_trailing_loss(symbol, purchase_date, perc):
             close = pdr.DataReader(symbol, 'yahoo', start=purchase_date)['Close']
             return close.cummax() * perc
 In[53]: tsla_cummax = tsla_close.cummax()
         tsla_cummax.head(8)
Out[53]: 
img_f33c1d1404eaa2a934294490406936d5.png

第01章 Pandas基础
第02章 DataFrame运算
第03章 数据分析入门
第04章 选取数据子集
第05章 布尔索引
第06章 索引对齐
第07章 分组聚合、过滤、转换
第08章 数据清理
第09章 合并Pandas对象
第10章 时间序列分析
第11章 用Matplotlib、Pandas、Seaborn进行可视化


目录
相关文章
|
11天前
|
数据采集 数据挖掘 数据处理
使用Python和Pandas进行数据分析基础
使用Python和Pandas进行数据分析基础
31 5
|
22天前
|
机器学习/深度学习 数据可视化 数据挖掘
使用Python进行数据分析的入门指南
【8月更文挑战第29天】在数据驱动的时代,掌握数据分析技能变得尤为重要。本文将引导您了解如何使用Python这一强大工具来进行数据分析,从设置开发环境到实际操作案例,逐步深入,帮助您建立起数据分析的基本框架和思维模式。通过阅读本文,您将学会如何利用Python处理、分析和可视化数据,为进一步深入学习奠定坚实的基础。
|
8天前
|
机器学习/深度学习 数据可视化 数据挖掘
深入浅出:使用Python进行数据分析的入门指南
【9月更文挑战第11天】本文旨在为初学者提供一条清晰的道路,通过Python探索数据科学的奇妙世界。我们将从基础语法讲起,逐步深入到数据处理、可视化以及机器学习等高级话题。文章不仅分享理论知识,还将通过实际代码示例,展示如何应用这些知识解决实际问题。无论你是编程新手,还是希望扩展技能的数据分析师,这篇文章都将是你宝贵的资源。
|
18天前
|
数据采集 数据可视化 数据挖掘
数据分析大神养成记:Python+Pandas+Matplotlib助你飞跃!
【9月更文挑战第2天】数据分析大神养成记:Python+Pandas+Matplotlib助你飞跃!
44 5
|
21天前
|
SQL 数据挖掘 关系型数据库
性能碾压pandas、polars的数据分析神器来了
性能碾压pandas、polars的数据分析神器来了
|
23天前
|
JSON 数据挖掘 API
案例 | 用pdpipe搭建pandas数据分析流水线
案例 | 用pdpipe搭建pandas数据分析流水线
|
29天前
|
数据挖掘 大数据 数据处理
数据分析师的秘密武器:精通Pandas DataFrame合并与连接技巧
【8月更文挑战第22天】在数据分析中,Pandas库的DataFrame提供高效的数据合并与连接功能。本文通过实例展示如何按员工ID合并基本信息与薪资信息,并介绍如何基于多列(如员工ID与部门ID)进行更复杂的连接操作。通过调整`merge`函数的`how`参数(如&#39;inner&#39;、&#39;outer&#39;等),可实现不同类型的连接。此外,还介绍了使用`join`方法根据索引快速连接数据,这对于处理大数据集尤其有用。掌握这些技巧能显著提升数据分析的能力。
46 1
|
30天前
|
算法 数据挖掘 数据处理
豆瓣评分8.7!Python pandas创始人亲码的数据分析入门手册!
在众多解释型语言中,Python最大的特点是拥有一个巨大而活跃的科学计算社区。进入21世纪以来,在行业应用和学术研究中采用python进行科学计算的势头越来越猛。 近年来,由于Python有不断改良的库(主要是pandas),使其成为数据处理任务的一大代替方案,结合其在通用编程方面的强大实力,完全可以只使用Python这一种语言去构建以数据为中心的应用程序。 作为一个科学计算平台,Python的成功源于能够轻松的集成C、C++以及Fortran代码。大部分现代计算机环境都利用了一些Fortran和C库来是西安线性代数、优选、积分、快速傅里叶变换以及其他诸如此类的算法。
|
20天前
|
数据采集 机器学习/深度学习 数据可视化
使用Python进行数据分析的入门指南
【8月更文挑战第30天】本文将引领读者进入Python在数据分析领域的应用,通过实际案例和代码示例,帮助初学者理解并掌握数据处理、可视化以及模型构建的基本技能。我们将从数据探索开始,逐步深入到数据清洗、分析和预测建模,最后以一个综合案例结束,旨在让读者能够独立完成一个数据分析项目。
|
22天前
|
存储 数据可视化 前端开发
7个Pandas&Jupyter特殊技巧,让Python数据分析更轻松
7个Pandas&Jupyter特殊技巧,让Python数据分析更轻松

热门文章

最新文章