python进阶-15.pandas-汇总计算,描述统计-唯一值,值计算-缺失值处理
2020-08-16 20:42
369 查看
文章目录
import pandas as pd
import numpy as np
from pandas import Series,DataFrame
3.7 汇总计算 与 描述统计
- pandas 的 Series 和 DataFrame 自带
-
xxx.sum()
-
xxx.mean()
-
xxx.max()
-
xxx.add()
-
等等等 方法
df1=DataFrame( [ [3,2,np.nan], [2,7,-5], [5,np.nan,np.nan], [7,6,4] ] ) df1
0 1 2 0 3 2.0 NaN 1 2 7.0 -5.0 2 5 NaN NaN 3 7 6.0 4.0
df1.sum()
0 17.0 1 15.0 2 -1.0 dtype: float64
-
默认参数 axis=0
df1.sum(axis=0)
0 17.0
1 15.0
2 -1.0
dtype: float64 -
axis=0 还可以携程 axis=‘index’
df1.sum(axis='index')
0 17.0 1 15.0 2 -1.0 dtype: float64
df1.sum(axis='columns')
0 5.0 1 4.0 2 5.0 3 17.0 dtype: float64
- #平均
print(df1) df1.mean(skipna=True)
0 1 2 0 3 2.0 NaN 1 2 7.0 -5.0 2 5 NaN NaN 3 7 6.0 4.0 0 4.25 1 5.00 2 -0.50 dtype: float64
- 让 nan 参与计算
df1.mean(skipna=False)
0 4.25 1 NaN 2 NaN dtype: float64
- 而很多时候, 需要处理nan,然后再计算
df1.fillna(0).mean(0)
0 4.25 1 3.75 2 -0.25 dtype: float64
累计求和
df2=DataFrame(np.random.randint(1,10,(4,6)),columns=list('ABCDEF')) df2
A B C D E F 0 9 9 3 8 8 1 1 8 4 1 9 3 4 2 8 1 2 7 5 8 3 6 5 6 4 4 9
- #普通求和
df2.sum(axis=1)
0 38 1 29 2 31 3 34 dtype: int64
- 累计求和
df2.cumsum(axis=1)
A B C D E F 0 9 18 21 29 37 38 1 8 12 13 22 25 29 2 8 9 11 18 23 31 3 6 11 17 21 25 34
- 最大值
print(df2) df2.cummax(axis=1)
A B C D E F 0 9 9 3 8 8 1 1 8 4 1 9 3 4 2 8 1 2 7 5 8 3 6 5 6 4 4 9 A B C D E F 0 9 9 9 9 9 9 1 8 8 8 9 9 9 2 8 8 8 8 8 8 3 6 6 6 6 6 9
描述统计
df3=DataFrame( np.random.randint(1,100,[10,4]), columns=list('ABCD')) df3
A B C D 0 33 4 30 81 1 98 50 94 26 2 66 85 36 35 3 28 10 89 90 4 78 1 76 49 5 1 83 39 61 6 30 16 55 9 7 12 75 59 83 8 41 49 13 22 9 74 37 40 93
- xxx.describe() 显示一堆信息
df3.describe()
A B C D count 10.000000 10.000000 10.000000 10.000000 mean 46.100000 41.000000 53.100000 54.900000 std 31.388073 32.612199 26.518128 30.996236 min 1.000000 1.000000 13.000000 9.000000 25% 28.500000 11.500000 36.750000 28.250000 50% 37.000000 43.000000 47.500000 55.000000 75% 72.000000 68.750000 71.750000 82.500000 max 98.000000 85.000000 94.000000 93.000000
df3.A.describe()
count 10.000000 mean 46.100000 std 31.388073 min 1.000000 25% 28.500000 50% 37.000000 75% 72.000000 max 98.000000 Name: A, dtype: float64
- 如果不是纯数字的 Series
s1 = Series( [np.random.choice('西瓜,橘子,苹果,木瓜,双星,77'.split(','))for i in range(60)] ) s1
0 苹果 1 77 2 木瓜 3 双星 4 西瓜 5 木瓜 ..... 55 77 56 苹果 57 橘子 58 苹果 59 橘子 dtype: object
s1.describe()
count 60 unique 6 top 双星 freq 13 dtype: object
3.8 唯一值,值计数,成员资格
- Series.unique() 提取唯一值
- 注意:DataFrame 没有
- 得到一个数组
s1.unique()
array(['苹果', '77', '木瓜', '双星', '西瓜', '橘子'], dtype=object) df2
A B C D E F 0 9 9 3 8 8 1 1 8 4 1 9 3 4 2 8 1 2 7 5 8 3 6 5 6 4 4 9
- 应用一个唯一值提取的函数
df2.apply(Series.unique)
A [9, 8, 6] B [9, 4, 1, 5] C [3, 1, 2, 6] D [8, 9, 7, 4] E [8, 3, 5, 4] F [1, 4, 8, 9] dtype: object
df2.apply(Series.unique,axis=1)
0 [9, 3, 8, 1] 1 [8, 4, 1, 9, 3] 2 [8, 1, 2, 7, 5] 3 [6, 5, 4, 9] dtype: object
- value count
s1.value_counts()
双星 13 西瓜 12 橘子 10 苹果 10 77 8 木瓜 7 dtype: int64
value count
s1.value_counts(normalize=True)
双星 0.216667 西瓜 0.200000 橘子 0.166667 苹果 0.166667 77 0.133333 木瓜 0.116667 dtype: float64
s1.value_counts().plot(kind='bar')
AxesSubplot:
!!! Series.value_counts()用于统计每个值的数量,默认按照数量的降序排序
- value_counts(self, normalize=False, sort=True, ascending=False,
bins=None, dropna=True)
参数:
- normaliz:bool,默认 False.如果是True,则返回的是 包含唯一值的相关频率
- sort:bool . 默认是True 是否按照值排序
- ascending:bool 默认FALSE 降序
- bins: 整数,或区间列表 分组数据,为pd.cut() 提供便利
- dropna: bool 默认为True , 默认丢掉 nan
s1.value_counts()
双星 13 西瓜 12 橘子 10 苹果 10 77 8 木瓜 7 dtype: int64
显示频率
s1.value_counts(normalize=True)
双星 0.216667 西瓜 0.200000 橘子 0.166667 苹果 0.166667 77 0.133333 木瓜 0.116667 dtype: float64
- #sort 是否按照值排序
s1.value_counts(sort=False)
木瓜 7 77 8 西瓜 12 双星 13 苹果 10 橘子 10 dtype: int64
bins 参数 , 用在数字数据上,做分区间统计
- 假设,统计 100 个人 的年龄。想统计不同年龄区间有多少人?
ages= Series(np.random.randint(1,101,100)) ages
0 20 1 9 2 11 3 65 4 11 .. 95 67 96 15 97 24 98 35 99 64 Length: 100, dtype: int32
- 当bins=整数,会自动找出最小值与最大值 。 每个区间的长度为 极差/bins
ages.value_counts(bins=4)
(51.0, 75.5] 29 (1.901, 26.5] 26 (26.5, 51.0] 25 (75.5, 100.0] 20 dtype: int64
- 当 bins=[a,b,c,d] 会统计 a-b,b-c,c-d 区间的人数
ages.value_counts(bins=[1,18,40,60,100])
(60.0, 100.0] 40 (40.0, 60.0] 20 (18.0, 40.0] 20 (0.999, 18.0] 20 dtype: int64
- 可以不包含 所有数据
ages.value_counts(normalize=True,bins=[18,40,60])
(17.999, 40.0] 0.21 (40.0, 60.0] 0.20 dtype: float64
ages.value_counts(normalize=True,bins=[18,40,60]).apply(lambda x :f'{x*100}%')
(17.999, 40.0] 21.0% (40.0, 60.0] 20.0% dtype: object
!!! 还有一个值统计函数 pandas.value_count()
- pandas.value_count(object,normalize=False, sort=True,
ascending=False, bins=None, dropna=True)
pd.value_counts(ages)
4 4 27 4 20 3 74 3 6 3 .. 58 1 99 1 52 1 51 1 2 1 Length: 65, dtype: int64
pd.value_counts(s1)
双星 13 西瓜 12 橘子 10 苹果 10 77 8 木瓜 7 dtype: int64
- 创建df
df4= DataFrame( np.random.randint(1,6,(4,6)), index=list('ABCD'), columns=list('甲乙丙丁戊已') ) df4
甲 乙 丙 丁 戊 已 A 1 5 2 4 2 3 B 5 1 1 2 3 4 C 2 1 3 5 2 4 D 1 5 1 3 4 3
df4.value_counts()
甲 乙 丙 丁 戊 已 5 1 1 2 3 4 1 2 1 3 5 2 4 1 1 5 2 4 2 3 1 1 3 4 3 1 dtype: int64
df4.apply(pd.value_counts,axis=0)
甲 乙 丙 丁 戊 已 1 2.0 2.0 2.0 NaN NaN NaN 2 1.0 NaN 1.0 1.0 2.0 NaN 3 NaN NaN 1.0 1.0 1.0 2.0 4 NaN NaN NaN 1.0 1.0 2.0 5 1.0 2.0 NaN 1.0 NaN NaN
df4.apply(pd.value_counts,axis=1)
1 2 3 4 5 A 1.0 2.0 1.0 1.0 1.0 B 2.0 1.0 1.0 1.0 1.0 C 1.0 2.0 1.0 1.0 1.0 D 2.0 NaN 2.0 1.0 1.0
成员资格(注意:巧的是本人在这个地方运行刚好没有出现2和4)
- object.isin([xx]) 判断xx是否在object中
s2=Series(np.random.randint(1,6,10)) s2
0 1 1 1 2 1 3 3 4 3 5 1 6 1 7 5 8 5 9 5 dtype: int32
- #判断后得到bool 序列
condition=s2.isin([2,4]) condition
0 False 1 False 2 False 3 False 4 False 5 False 6 False 7 False 8 False 9 False dtype: bool
s2[condition]
Series([], dtype: int32)
s2.isin([2,4]).apply(lambda x:not x)
0 True 1 True 2 True 3 True 4 True 5 True 6 True 7 True 8 True 9 True dtype: bool
- 读取 s2 , 2, 4 以外的数
- 应用匿名函数 对每个值 进行 not 操作
s2[s2.isin([2,4]).apply(lambda x:not x)]
0 1 1 1 2 1 3 3 4 3 5 1 6 1 7 5 8 5 9 5 dtype: int32
~s2.isin([2,4])
0 True 1 True 2 True 3 True 4 True 5 True 6 True 7 True 8 True 9 True dtype: bool
s2.isin([2,4])^True
0 True 1 True 2 True 3 True 4 True 5 True 6 True 7 True 8 True 9 True dtype: bool
3.9 缺失值处理
-
dropna(axis=0,how =‘any’,inplace=False) 默认只要有一个nan就 丢弃数据
how=‘all’ 表示 整行或者整列全部都是nan才丢掉
-
fillna() 填充值
-
isnull() 返回 是 nan 的布尔序列
-
notnull() 返回不是nan 的布尔序列
df1
0 1 2 0 3 2.0 NaN 1 2 7.0 -5.0 2 5 NaN NaN 3 7 6.0 4.0
df1.dropna()
0 1 2 1 2 7.0 -5.0 3 7 6.0 4.0
df1.dropna(axis=1)
0 0 3 1 2 2 5 3 7
df1.dropna(how='all')
0 1 2 0 3 2.0 NaN 1 2 7.0 -5.0 2 5 NaN NaN 3 7 6.0 4.0
df1[1]
0 2.0 1 7.0 2 NaN 3 6.0 Name: 1, dtype: float64
df1[1].isnull()
0 False 1 False 2 True 3 False Name: 1, dtype: bool
df1[1].notnull()
0 True 1 True 2 False 3 True Name: 1, dtype: bool
df1[1].fillna(0)
0 2.0 1 7.0 2 0.0 3 6.0 Name: 1, dtype: float64
相关文章推荐
- python-pandas模块5-汇总计算-描述统计-唯一值-值计数-成员资格-缺失值处理
- python:pandas(3),汇总和计算描述统计
- 利用 Python 进行数据分析(九)pandas 汇总统计和计算
- Python从数据库取数,对时间进行处理,统计数据汇总后画图
- Python实现统计描述以及计算皮尔逊相关系数
- pandas带有重复值的轴索引,汇总和计算描述统计,相关系数与协方差
- python数据分析pandas包入门学习(三)汇总和统计描述
- python/pandas/Numpy数据分析-统计描述,唯一值,值计数
- pandas汇总和计算描述统计
- pandas之汇总和计算描述统计到层次化索引
- pandas的汇总和计算描述统计
- 【语言处理与Python】1.3计算语言:简单的统计
- python数据分析之Pandas:汇总和计算描述统计
- 利用Python进行数据分析(9) pandas基础: 汇总统计和计算
- pandas(三)汇总和计算描述统计
- Python 网页爬虫 & 文本处理 & 科学计算 & 机器学习 & 数据挖掘兵器谱
- 用Python作流场计算时的后处理问题
- Python进阶必读博文视频教程汇总
- python 科学计算学习一:numpy快速处理数据(3)
- 科学计算——概率统计计算机模拟——Python