ImapBox 课程推荐:《迈向数据科学家:带你玩转Python数据分析》,讲师齐伟,苏州研途教育科技有限公司CTO,苏州大学应用统计专业硕士生指导委员会委员;已出版《跟老齐学Python:轻松入门》《跟老齐学Python:Django实战》、《跟老齐学Python:数据分析》和《Python大学实用教程》畅销图书。 Pandas 系列文章(正在更新中…): 另有 NumPy、Matplotlib 系列文章已更新完毕,欢迎关注: 推荐学习资料与网站(博主参与部分文档翻译): Pandas 对象拥有一组常用的数学和统计方法。它们大部分都属于约简和汇总统计,用于从 Series 中提取单个值(如 sum 或 mean)或从 DataFrame 的行或列中提取一个 Series。跟对应的 NumPy 数组方法相比,它们都是基于没有缺失数据的假设而构建的。 在 Series 和 DataFrame 中的基本语法如下: 官方文档: https://pandas.pydata.org/docs/reference/api/pandas.Series.sum.html https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.sum.html 常用参数描述如下: 在 Series 中的应用: 在 DataFrame 中的应用: 在 Series 和 DataFrame 中的基本语法如下: 官方文档: https://pandas.pydata.org/docs/reference/api/pandas.Series.min.html https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.min.html 常用参数描述如下: 在 Series 中的应用: 在 DataFrame 中的应用: 在 Series 和 DataFrame 中的基本语法如下: 官方文档: https://pandas.pydata.org/docs/reference/api/pandas.Series.max.html https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.max.html 常用参数描述如下: 在 Series 中的应用: 在 DataFrame 中的应用: 在 Series 和 DataFrame 中的基本语法如下: 官方文档: https://pandas.pydata.org/docs/reference/api/pandas.Series.mean.html https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.mean.html 常用参数描述如下: 在 Series 中的应用: 在 DataFrame 中的应用: 在 Series 和 DataFrame 中的基本语法如下: 官方文档: https://pandas.pydata.org/docs/reference/api/pandas.Series.idxmin.html https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.idxmin.html 常用参数描述如下: 在 Series 中的应用: 在 DataFrame 中的应用: 在 Series 和 DataFrame 中的基本语法如下: 官方文档: https://pandas.pydata.org/docs/reference/api/pandas.Series.idxmax.html https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.idxmax.html 常用参数描述如下: 在 Series 中的应用: 在 DataFrame 中的应用: 在 Series 和 DataFrame 中的基本语法如下: 官方文档: https://pandas.pydata.org/docs/reference/api/pandas.Series.describe.html https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.describe.html 描述数字形式的 Series 对象: 分类描述: 描述时间戳: 描述 DataFrame 对象: 不考虑数据类型,显示所有描述: 仅包含 category 列: 其他常用统计方法参见下表:
文章目录
这里是一段防爬虫文本,请读者忽略。 本文原创首发于 ImapBox,作者 TRHX。 博客首页:https://itrhx.blog.csdn.net/ 本文链接:https://itrhx.blog.csdn.net/article/details/106788501 未经授权,禁止转载!恶意转载,后果自负!尊重原创,远离剽窃! 
【01×00】统计计算
【01×01】sum() 求和
sum() 方法用于返回指定轴的和,相当于 numpy.sum()。
Series.sum(self, axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)DataFrame.sum(self, axis=None, skipna=None, level=None, numeric_only=None, min_count=0, **kwargs)
 
参数 
描述 
 
axis 
指定轴求和, 
0 or ‘index’,1 or ‘columns’,只有在 DataFrame 中才有 1 or 'columns’
 
skipna 
bool 类型,求和时是否排除缺失值(NA/null),默认 True 
 
level 
如果轴是 MultiIndex(层次结构),则沿指定层次求和 
>>> import pandas as pd >>> idx = pd.MultiIndex.from_arrays([ ['warm', 'warm', 'cold', 'cold'], ['dog', 'falcon', 'fish', 'spider']],     names=['blooded', 'animal']) >>> obj = pd.Series([4, 2, 0, 8], name='legs', index=idx) >>> obj blooded  animal warm     dog       4          falcon    2 cold     fish      0          spider    8 Name: legs, dtype: int64 >>> >>> obj.sum() 14 >>> >>> obj.sum(level='blooded') blooded warm    6 cold    8 Name: legs, dtype: int64 >>> >>> obj.sum(level=0) blooded warm    6 cold    8 Name: legs, dtype: int64 >>> import pandas as pd >>> import numpy as np >>> obj = pd.DataFrame([[1.4, np.nan], [7.1, -4.5], [np.nan, np.nan], [0.75, -1.3]],     index=['a', 'b', 'c', 'd'],     columns=['one', 'two']) >>> obj     one  two a  1.40  NaN b  7.10 -4.5 c   NaN  NaN d  0.75 -1.3 >>> >>> obj.sum() one    9.25 two   -5.80 dtype: float64 >>> >>> obj.sum(axis=1) a    1.40 b    2.60 c    0.00 d   -0.55 dtype: float64 【01×02】min() 最小值
min() 方法用于返回指定轴的最小值。
Series.min(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs)DataFrame.min(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
 
参数 
描述 
 
axis 
指定轴求最小值, 
0 or ‘index’,1 or ‘columns’,只有在 DataFrame 中才有 1 or 'columns’
 
skipna 
bool 类型,求最小值时是否排除缺失值(NA/null),默认 True 
 
level 
如果轴是 MultiIndex(层次结构),则沿指定层次求最小值 
>>> import pandas as pd >>> idx = pd.MultiIndex.from_arrays([ ['warm', 'warm', 'cold', 'cold'], ['dog', 'falcon', 'fish', 'spider']],     names=['blooded', 'animal']) >>> obj = pd.Series([4, 2, 0, 8], name='legs', index=idx) >>> obj blooded  animal warm     dog       4          falcon    2 cold     fish      0          spider    8 Name: legs, dtype: int64 >>> >>> obj.min() 0 >>> >>> obj.min(level='blooded') blooded warm    2 cold    0 Name: legs, dtype: int64 >>> >>> obj.min(level=0) blooded warm    2 cold    0 Name: legs, dtype: int64 >>> import pandas as pd >>> import numpy as np >>> obj = pd.DataFrame([[1.4, np.nan], [7.1, -4.5], [np.nan, np.nan], [0.75, -1.3]],     index=['a', 'b', 'c', 'd'],columns=['one', 'two']) >>> obj     one  two a  1.40  NaN b  7.10 -4.5 c   NaN  NaN d  0.75 -1.3 >>> >>> obj.min() one    0.75 two   -4.50 dtype: float64 >>> >>> obj.min(axis=1) a    1.4 b   -4.5 c    NaN d   -1.3 dtype: float64 >>> >>> obj.min(axis='columns', skipna=False) a    NaN b   -4.5 c    NaN d   -1.3 dtype: float64 【01×03】max() 最大值
max() 方法用于返回指定轴的最大值。
Series.max(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs)DataFrame.max(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
 
参数 
描述 
 
axis 
指定轴求最大值, 
0 or ‘index’,1 or ‘columns’,只有在 DataFrame 中才有 1 or 'columns’
 
skipna 
bool 类型,求最大值时是否排除缺失值(NA/null),默认 True 
 
level 
如果轴是 MultiIndex(层次结构),则沿指定层次求最大值 
>>> import pandas as pd >>> idx = pd.MultiIndex.from_arrays([ ['warm', 'warm', 'cold', 'cold'], ['dog', 'falcon', 'fish', 'spider']],     names=['blooded', 'animal']) >>> obj = pd.Series([4, 2, 0, 8], name='legs', index=idx) >>> obj blooded  animal warm     dog       4          falcon    2 cold     fish      0          spider    8 Name: legs, dtype: int64 >>> >>> obj.max() 8 >>> >>> obj.max(level='blooded') blooded warm    4 cold    8 Name: legs, dtype: int64 >>> >>> obj.max(level=0) blooded warm    4 cold    8 Name: legs, dtype: int64 >>> import pandas as pd >>> import numpy as np >>> obj = pd.DataFrame([[1.4, np.nan], [7.1, -4.5], [np.nan, np.nan], [0.75, -1.3]],     index=['a', 'b', 'c', 'd'],columns=['one', 'two']) >>> obj     one  two a  1.40  NaN b  7.10 -4.5 c   NaN  NaN d  0.75 -1.3 >>> >>> obj.max() one    7.1 two   -1.3 dtype: float64 >>> >>> obj.max(axis=1) a    1.40 b    7.10 c     NaN d    0.75 dtype: float64 >>> >>> obj.max(axis='columns', skipna=False) a     NaN b    7.10 c     NaN d    0.75 dtype: float64 【01×04】mean() 平均值
mean() 方法用于返回指定轴的平均值。
Series.mean(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs)DataFrame.mean(self, axis=None, skipna=None, level=None, numeric_only=None, **kwargs)
 
参数 
描述 
 
axis 
指定轴求平均值, 
0 or ‘index’,1 or ‘columns’,只有在 DataFrame 中才有 1 or 'columns’
 
skipna 
bool 类型,求平均值时是否排除缺失值(NA/null),默认 True 
 
level 
如果轴是 MultiIndex(层次结构),则沿指定层次求平均值 
>>> import pandas as pd >>> idx = pd.MultiIndex.from_arrays([ ['warm', 'warm', 'cold', 'cold'], ['dog', 'falcon', 'fish', 'spider']],     names=['blooded', 'animal']) >>> obj = pd.Series([4, 2, 0, 8], name='legs', index=idx) >>> obj blooded  animal warm     dog       4          falcon    2 cold     fish      0          spider    8 Name: legs, dtype: int64 >>> >>> obj.mean() 3.5 >>> >>> obj.mean(level='blooded') blooded warm    3 cold    4 Name: legs, dtype: int64 >>> >>> obj.mean(level=0) blooded warm    3 cold    4 Name: legs, dtype: int64 >>> import pandas as pd >>> import numpy as np >>> obj = pd.DataFrame([[1.4, np.nan], [7.1, -4.5], [np.nan, np.nan], [0.75, -1.3]],     index=['a', 'b', 'c', 'd'],columns=['one', 'two']) >>> obj     one  two a  1.40  NaN b  7.10 -4.5 c   NaN  NaN d  0.75 -1.3 >>> >>> obj.mean() one    3.083333 two   -2.900000 dtype: float64 >>> >>> obj.mean(axis=1) a    1.400 b    1.300 c      NaN d   -0.275 dtype: float64 >>> >>> obj.mean(axis='columns', skipna=False) a      NaN b    1.300 c      NaN d   -0.275 dtype: float64 【01×05】idxmin() 最小值索引
idxmin() 方法用于返回最小值的索引。
Series.idxmin(self, axis=0, skipna=True, *args, **kwargs)DataFrame.idxmin(self, axis=0, skipna=True)
 
参数 
描述 
 
axis 
指定轴, 
0 or ‘index’,1 or ‘columns’,只有在 DataFrame 中才有 1 or 'columns’
 
skipna 
bool 类型,是否排除缺失值(NA/null),默认 True 
>>> import pandas as pd >>> idx = pd.MultiIndex.from_arrays([ ['warm', 'warm', 'cold', 'cold'], ['dog', 'falcon', 'fish', 'spider']],     names=['blooded', 'animal']) >>> obj = pd.Series([4, 2, 0, 8], name='legs', index=idx) >>> obj blooded  animal warm     dog       4          falcon    2 cold     fish      0          spider    8 Name: legs, dtype: int64 >>> >>> obj.idxmin() ('cold', 'fish') >>> import pandas as pd >>> import numpy as np >>> obj = pd.DataFrame([[1.4, np.nan], [7.1, -4.5], [np.nan, np.nan], [0.75, -1.3]],     index=['a', 'b', 'c', 'd'],columns=['one', 'two']) >>> obj     one  two a  1.40  NaN b  7.10 -4.5 c   NaN  NaN d  0.75 -1.3 >>> >>> obj.idxmin() one    d two    b dtype: object 【01×06】idxmax() 最大值索引
idxmax() 方法用于返回最大值的索引。
Series.idxmax(self, axis=0, skipna=True, *args, **kwargs)DataFrame.idxmax(self, axis=0, skipna=True)
 
参数 
描述 
 
axis 
指定轴, 
0 or ‘index’,1 or ‘columns’,只有在 DataFrame 中才有 1 or 'columns’
 
skipna 
bool 类型,是否排除缺失值(NA/null),默认 True 
>>> import pandas as pd >>> idx = pd.MultiIndex.from_arrays([ ['warm', 'warm', 'cold', 'cold'], ['dog', 'falcon', 'fish', 'spider']],     names=['blooded', 'animal']) >>> obj = pd.Series([4, 2, 0, 8], name='legs', index=idx) >>> obj blooded  animal warm     dog       4          falcon    2 cold     fish      0          spider    8 Name: legs, dtype: int64 >>> >>> obj.idxmax() ('cold', 'spider') >>> import pandas as pd >>> import numpy as np >>> obj = pd.DataFrame([[1.4, np.nan], [7.1, -4.5], [np.nan, np.nan], [0.75, -1.3]],     index=['a', 'b', 'c', 'd'],columns=['one', 'two']) >>> obj     one  two a  1.40  NaN b  7.10 -4.5 c   NaN  NaN d  0.75 -1.3 >>> >>> obj.idxmax() one    b two    d dtype: object 【02×00】统计描述
describe() 方法用于快速综合统计结果:计数、均值、标准差、最大最小值、四分位数等。还可以通过参数来设置需要忽略或者包含的统计选项。
Series.describe(self: ~ FrameOrSeries, percentiles=None, include=None, exclude=None)DataFrame.describe(self: ~ FrameOrSeries, percentiles=None, include=None, exclude=None)
 
参数 
描述 
 
percentiles 
数字列表,可选项,要包含在输出中的百分比。所有值都应介于 0 和 1 之间。默认值为 [.25、.5、.75],即返回第 25、50 和 75 个百分点 
 
include 
要包含在结果中的数据类型,数据类型列表,默认 None,具体取值类型参见官方文档 
 
exclude 
要从结果中忽略的数据类型,数据类型列表,默认 None,具体取值类型参见官方文档 
>>> import pandas as pd >>> obj = pd.Series([1, 2, 3]) >>> obj 0 1 1 2 2 3 dtype: int64 >>> >>> obj.describe() count    3.0 mean     2.0 std      1.0 min 1.0 25% 1.5 50% 2.0 75% 2.5 max 3.0 dtype: float64 >>> import pandas as pd >>> obj = pd.Series(['a', 'a', 'b', 'c']) >>> obj 0    a 1    a 2    b 3    c dtype: object >>> >>> obj.describe() count     4 unique    3 top       a freq      2 dtype: object >>> import pandas as pd >>> obj  = pd.Series([     np.datetime64("2000-01-01"),     np.datetime64("2010-01-01"),     np.datetime64("2010-01-01") ]) >>> obj 0 2000-01-01 1 2010-01-01 2 2010-01-01 dtype: datetime64[ns] >>> >>> obj.describe() count                       3 unique                      2 top       2010-01-01 00:00:00 freq                        2 first     2000-01-01 00:00:00 last      2010-01-01 00:00:00 dtype: object >>> import pandas as pd >>> obj = pd.DataFrame({'categorical': pd.Categorical(['d','e','f']), 'numeric': [1, 2, 3], 'object': ['a', 'b', 'c']}) >>> obj   categorical  numeric object 0           d        1      a 1           e        2      b 2           f        3      c >>> >>> obj.describe()        numeric count      3.0 mean       2.0 std        1.0 min 1.0 25% 1.5 50% 2.0 75% 2.5 max 3.0 >>> import pandas as pd >>> obj = pd.DataFrame({'categorical': pd.Categorical(['d','e','f']), 'numeric': [1, 2, 3], 'object': ['a', 'b', 'c']}) >>> obj   categorical  numeric object 0           d        1      a 1           e        2      b 2           f        3      c >>> >>> obj.describe(include='all')        categorical  numeric object count            3 3.0 3 unique           3      NaN      3 top              f      NaN      c freq             1      NaN      1 mean           NaN      2.0    NaN std            NaN      1.0    NaN min            NaN      1.0    NaN 25%            NaN      1.5    NaN 50%            NaN      2.0    NaN 75%            NaN      2.5    NaN max            NaN      3.0    NaN >>> import pandas as pd >>> obj = pd.DataFrame({'categorical': pd.Categorical(['d','e','f']), 'numeric': [1, 2, 3], 'object': ['a', 'b', 'c']}) >>> obj   categorical  numeric object 0           d        1      a 1           e        2      b 2           f        3      c >>> >>> obj.describe(include=['category'])        categorical count            3 unique           3 top              f freq             1 【03×00】常用统计方法
 
方法 
描述 
官方文档 
 
count 
非NA值的数量 
Series丨DataFrame 
 
describe 
针对Series或各DataFrame列计算汇总统计 
Series丨DataFrame 
 
min 
计算最小值 
Series丨DataFrame 
 
max 
计算最大值 
Series丨DataFrame 
 
argmin 
计算能够获取到最小值的索引位置(整数) 
Series 
 
argmax 
计算能够获取到最大值的索引位置(整数) 
Series 
 
idxmin 
计算能够获取到最小值的索引值 
Series丨DataFrame 
 
idxmax 
计算能够获取到最大值的索引值 
Series丨DataFrame 
 
quantile 
计算样本的分位数(0到1) 
Series丨DataFrame 
 
sum 
值的总和 
Series丨DataFrame 
 
mean 
值的平均数 
Series丨DataFrame 
 
median 
值的算术中位数(50%分位数) 
Series丨DataFrame 
 
mad 
根据平均值计算平均绝对离差 
Series丨DataFrame 
 
var 
样本值的方差 
Series丨DataFrame 
 
std 
样本值的标准差 
Series丨DataFrame 
这里是一段防爬虫文本,请读者忽略。 本文原创首发于 ImapBox,作者 TRHX。 博客首页:https://itrhx.blog.csdn.net/ 本文链接:https://itrhx.blog.csdn.net/article/details/106788501 未经授权,禁止转载!恶意转载,后果自负!尊重原创,远离剽窃! 
 
本网页所有视频内容由 imoviebox边看边下-网页视频下载, iurlBox网页地址收藏管理器 下载并得到。
ImovieBox网页视频下载器 下载地址: ImovieBox网页视频下载器-最新版本下载
本文章由: imapbox邮箱云存储,邮箱网盘,ImageBox 图片批量下载器,网页图片批量下载专家,网页图片批量下载器,获取到文章图片,imoviebox网页视频批量下载器,下载视频内容,为您提供.
阅读和此文章类似的: 全球云计算
 官方软件产品操作指南 (170)
官方软件产品操作指南 (170)