我是靠谱客的博主 纯真皮卡丘,这篇文章主要介绍Pandas“总结函数”与”映射“Summary FunctionsMaps,现在分享给大家,希望可以做个参考。

pandas的"总结函数"和"映射"

  • Summary Functions
  • Maps
    • map()方法
    • apply方法

Summary Functions

pandas 提供了一些简单的“总结函数",如describle()方法,mean()等
**df.describle()**可以对一列数据进行简单的数据统计
df.mean() 获得一列数据的平均值
df.unique() 统计一列数据的唯一值
df.val_counts() 统计某一个值出现的次数

import pandas as pd
import math
data = pd.DataFrame({"country":["Russian","China","America","Brazil","China","Japan","America"],"plane":[1170, 960, 980,800,math.nan,300,3244],"Population":[1,13,math.nan,2,14,3,5]})
data
"""
country	plane	Population
0	Russian	1170.0	1.0
1	China	960.0	13.0
2	America	980.0	NaN
3	Brazil	800.0	2.0
4	China	NaN	14.0
5	Japan	300.0	3.0
6	America	3244.0	5.0
"""
data.plane.describe()
#output
"""
count
6.000000
mean
1242.333333
std
1024.117506
min
300.000000
25%
840.000000
50%
970.000000
75%
1122.500000
max
3244.000000
Name: plane, dtype: float64
"""
data.plane.unique()
#output
"""
array([1170.,
960.,
980.,
800.,
nan,
300., 3244.])
"""
data.plane.mean()
#output
"""
1242.3333333333333
"""
data.plane.val_counts()
#output
"""
3244.0
1
960.0
1
300.0
1
800.0
1
980.0
1
1170.0
1
Name: plane, dtype: int64
"""

Maps

映射是从数学上借过来的术语,指输入一系列的值输入到函数中然后将它们”映射“为另一系列的值。在处理数据的时候,我们通常要将已有的数据变换为我们在后续工作中需要的形式。映射便是这么一个工作
这里介绍两个常用的映射方法:

map()方法


# 对plane的所有值减去平均值
planes_mean = data.plane.mean()
data.plane.map(lambda p : p - planes_mean)
# output
"""
0
-72.333333
1
-282.333333
2
-262.333333
3
-442.333333
4
NaN
5
-942.333333
6
2001.666667
Name: plane, dtype: float64
"""

map()的输入函数的输入是一个来自Series的值,然后返回变形后的值。

apply方法

# 通过对每一行的数据使用apply()方法将整个 DataFrame 进行转换。
def remean_plane(row):
row.plane =
row.plane - planes_mean
return row
data.apply(remean_plane, axis="columns)
# output
"""
country	plane	Population
0	Russian	-72.333333	1.0
1	China	-282.333333	13.0
2	America	-262.333333	NaN
3	Brazil	-442.333333	2.0
4	China	NaN	14.0
5	Japan	-942.333333	3.0
6	America	2001.666667	5.0
"""

Note: maps() 返回变形后的Series,apply()返回变形后的dataframes,两者都没有改变原始数据。

最后

以上就是纯真皮卡丘最近收集整理的关于Pandas“总结函数”与”映射“Summary FunctionsMaps的全部内容,更多相关Pandas“总结函数”与”映射“Summary内容请搜索靠谱客的其他文章。

本图文内容来源于网友提供,作为学习参考使用,或来自网络收集整理,版权属于原作者所有。
点赞(48)

评论列表共有 0 条评论

立即
投稿
返回
顶部