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
012
032.0NaN
127.0-5.0
25NaNNaN
376.04.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
ABCDEF
0993881
1841934
2812758
3656449
#普通求和
df2.sum(axis=1)
0 38
1 29
2 31
3 34
dtype: int64
累计求和
df2.cumsum(axis=1)
ABCDEF
091821293738
181213222529
28911182331
361117212534
最大值
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
ABCDEF
0999999
1888999
2888888
3666669
描述统计
df3=DataFrame(
np.random.randint(1,100,[10,4]),
columns=list('ABCD'))
df3
ABCD
03343081
198509426
266853635
328108990
47817649
51833961
63016559
712755983
841491322
974374093
xxx.describe() 显示一堆信息
df3.describe()
ABCD
count10.00000010.00000010.00000010.000000
mean46.10000041.00000053.10000054.900000
std31.38807332.61219926.51812830.996236
min1.0000001.00000013.0000009.000000
25%28.50000011.50000036.75000028.250000
50%37.00000043.00000047.50000055.000000
75%72.00000068.75000071.75000082.500000
max98.00000085.00000094.00000093.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
ABCDEF
0993881
1841934
2812758
3656449
应用一个唯一值提取的函数
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')
!!! 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
甲乙丙丁戊已
A152423
B511234
C213524
D151343
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)
甲乙丙丁戊已
12.02.02.0NaNNaNNaN
21.0NaN1.01.02.0NaN
3NaNNaN1.01.01.02.0
4NaNNaNNaN1.01.02.0
51.02.0NaN1.0NaNNaN
df4.apply(pd.value_counts,axis=1)
12345
A1.02.01.01.01.0
B2.01.01.01.01.0
C1.02.01.01.01.0
D2.0NaN2.01.01.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
012
032.0NaN
127.0-5.0
25NaNNaN
376.04.0
df1.dropna()
012
127.0-5.0
376.04.0
df1.dropna(axis=1)
0
03
12
25
37
df1.dropna(how='all')
012
032.0NaN
127.0-5.0
25NaNNaN
376.04.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
原文链接:https://blog.csdn.net/weixin_47440383/article/details/108042412