【Python】6000字、22个案例详解Pandas数据分析/预处理时的实用技巧,超简单
机器学习初学者
共 8637字,需浏览 18分钟
· 2022-01-19
Pandas
在数据预处理和数据分析方面的硬核干货,我们大致会说Pandas
计算交叉列表Pandas
将字符串与数值转化成时间类型Pandas
将字符串转化成数值类型
Pandas
当中的交叉列表
Pandas
模块当中的crosstab()
函数,它的作用主要是进行分组之后的信息统计,里面会用到聚合函数,默认的是统计行列组合出现的次数,参数如下pandas.crosstab(index, columns,
values=None,
rownames=None,
colnames=None,
aggfunc=None,
margins=False,
margins_name='All',
dropna=True,
normalize=False)
下面小编来解释一下里面几个常用的函数
index: 指定了要分组的类目,作为行 columns: 指定了要分组的类目,作为列 rownames/colnames: 行/列的名称 aggfunc: 指定聚合函数 values: 最终在聚合函数之下,行与列一同计算出来的值 normalize: 标准化统计各行各列的百分比
corss_tab()
函数的作用,我们先导入要用到的模块并且读取数据集import pandas as pd
df = pd.read_excel(
io="supermarkt_sales.xlsx",
engine="openpyxl",
sheet_name="Sales",
skiprows=3,
usecols="B:R",
nrows=1000,
)
output
我们先简单来看几个corsstab()
函数的例子,代码如下
pd.crosstab(df['城市'], df['顾客类型'])
output
顾客类型 会员 普通
省份
上海 124 115
北京 116 127
四川 26 35
安徽 28 12
广东 30 36
.......
当然我们这里只是指定了一个列,也可以指定多个,代码如下
pd.crosstab(df['省份'], [df['顾客类型'], df["性别"]])
output
顾客类型 会员 普通
性别 女性 男性 女性 男性
省份
上海 67 57 53 62
北京 53 63 59 68
四川 17 9 16 19
安徽 17 11 9 3
广东 18 12 15 21
.....
有时候我们想要改变行索引的名称或者是列方向的名称,我们则可以这么做
pd.crosstab(df['省份'], df['顾客类型'],
colnames = ['顾客的类型'],
rownames = ['各省份名称'])
output
顾客的类型 会员 普通
各省份名称
上海 124 115
北京 116 127
四川 26 35
安徽 28 12
广东 30 36
crosstab()
方法当中的margin
参数,如下pd.crosstab(df['省份'], df['顾客类型'], margins = True)
output
顾客类型 会员 普通 All
省份
上海 124 115 239
北京 116 127 243
.....
江苏 18 15 33
浙江 119 111 230
黑龙江 14 17 31
All 501 499 1000
你也可以给汇总的那一列重命名,用到的是margins_name
参数,如下
pd.crosstab(df['省份'], df['顾客类型'],
margins = True, margins_name="汇总")
output
顾客类型 会员 普通 汇总
省份
上海 124 115 239
北京 116 127 243
.....
江苏 18 15 33
浙江 119 111 230
黑龙江 14 17 31
汇总 501 499 1000
normalize
参数,如下pd.crosstab(df['省份'], df['顾客类型'],
normalize=True)
output
顾客类型 会员 普通
省份
上海 0.124 0.115
北京 0.116 0.127
四川 0.026 0.035
安徽 0.028 0.012
广东 0.030 0.036
.......
要是我们更加倾向于是百分比,并且保留两位小数,则可以这么来做
pd.crosstab(df['省份'], df['顾客类型'],
normalize=True).style.format('{:.2%}')
output
顾客类型 会员 普通
省份
上海 12.4% 11.5%
北京 11.6% 12.7%
四川 26% 35%
安徽 28% 12%
广东 30% 36%
.......
aggfunc
参数以及values
参数,代码如下pd.crosstab(df['省份'], df['顾客类型'],
values = df["总收入"],
aggfunc = "mean")
output
顾客类型 会员 普通
省份
上海 15.648738 15.253248
北京 14.771259 14.354390
四川 20.456135 14.019029
安徽 10.175893 11.559917
广东 14.757083 18.331903
.......
np.sum
加总或者是np.median
求取平均值。我们还可以指定保留若干位的小数,使用round()
函数
df_1 = pd.crosstab(df['省份'], df['顾客类型'],
values=df["总收入"],
aggfunc="mean").round(2)
output
顾客类型 会员 普通
省份
上海 15.65 15.25
北京 14.77 14.35
四川 20.46 14.02
安徽 10.18 11.56
广东 14.76 18.33
.......
时间类型数据的转化
df = pd.DataFrame({'date': [1470195805, 1480195805, 1490195805],
'value': [2, 3, 4]})
pd.to_datetime(df['date'], unit='s')
output
0 2016-08-03 03:43:25
1 2016-11-26 21:30:05
2 2017-03-22 15:16:45
Name: date, dtype: datetime64[ns]
上面的例子是精确到秒,我们也可以精确到天,代码如下
df = pd.DataFrame({'date': [1470, 1480, 1490],
'value': [2, 3, 4]})
pd.to_datetime(df['date'], unit='D')
output
0 1974-01-10
1 1974-01-20
2 1974-01-30
Name: date, dtype: datetime64[ns]
pd.to_datetime()
方法pd.to_datetime('2022/01/20', format='%Y/%m/%d')
output
Timestamp('2022-01-20 00:00:00')
亦或是
pd.to_datetime('2022/01/12 11:20:10',
format='%Y/%m/%d %H:%M:%S')
output
Timestamp('2022-01-12 11:20:10')
这里着重介绍一下Python
当中的时间日期格式化符号
%y 两位数的年份表示(00-99) %Y 四位数的年份表示(000-9999) %m 表示的是月份(01-12) %d 表示的是一个月当中的一天(0-31) %H 表示的是24小时制的小时数 %I 表示的是12小时制的小时数 %M 表示的是分钟数 (00-59) %S 表示的是秒数(00-59) %w 表示的是星期数,一周当中的第几天,从星期天开始算 %W 表示的是一年中的星期数
pd.to_datetime()
方法当中的errors
参数就可以派上用场,df = pd.DataFrame({'date': ['3/10/2000', 'a/11/2000', '3/12/2000'],
'value': [2, 3, 4]})
# 会报解析错误
df['date'] = pd.to_datetime(df['date'])
output
我们来看一下errors
参数的作用,代码如下
df['date'] = pd.to_datetime(df['date'], errors='ignore')
df
output
date value
0 3/10/2000 2
1 a/11/2000 3
2 3/12/2000 4
或者将不准确的值转换成NaT
,代码如下
df['date'] = pd.to_datetime(df['date'], errors='coerce')
df
output
date value
0 2000-03-10 2
1 NaT 3
2 2000-03-12 4
数值类型的转换
DataFrame
数据集,如下df = pd.DataFrame({
'string_col': ['1','2','3','4'],
'int_col': [1,2,3,4],
'float_col': [1.1,1.2,1.3,4.7],
'mix_col': ['a', 2, 3, 4],
'missing_col': [1.0, 2, 3, np.nan],
'money_col': ['£1,000.00','£2,400.00','£2,400.00','£2,400.00'],
'boolean_col': [True, False, True, True],
'custom': ['Y', 'Y', 'N', 'N']
})
output
我们先来查看一下每一列的数据类型
df.dtypes
output
string_col object
int_col int64
float_col float64
mix_col object
missing_col float64
money_col object
boolean_col bool
custom object
dtype: object
df.info()
方法来调用,如下df.info()
output
'pandas.core.frame.DataFrame'>
RangeIndex: 4 entries, 0 to 3
Data columns (total 8 columns):
# Column Non-Null Count Dtype
--- ------ -------------- -----
0 string_col 4 non-null object
1 int_col 4 non-null int64
2 float_col 4 non-null float64
3 mix_col 4 non-null object
4 missing_col 3 non-null float64
5 money_col 4 non-null object
6 boolean_col 4 non-null bool
7 custom 4 non-null object
dtypes: bool(1), float64(2), int64(1), object(4)
memory usage: 356.0+ bytes
我们先来看一下从字符串到整型数据的转换,代码如下
df['string_col'] = df['string_col'].astype('int')
df.dtypes
output
string_col int32
int_col int64
float_col float64
mix_col object
missing_col float64
money_col object
boolean_col bool
custom object
dtype: object
int32
类型,当然我们指定例如astype('int16')
、astype('int8')
或者是astype('int64')
,当我们碰到量级很大的数据集时,会特别的有帮助。那么类似的,我们想要转换成浮点类型的数据,就可以这么来做
df['string_col'] = df['string_col'].astype('float')
df.dtypes
output
string_col float64
int_col int64
float_col float64
mix_col object
missing_col float64
money_col object
boolean_col bool
custom object
dtype: object
astype('float16')
、astype('float32')
或者是astype('float128')
df['mix_col'] = df['mix_col'].astype('int')
output
pd.to_numeric()
方法以及里面的errors
参数,代码如下df['mix_col'] = pd.to_numeric(df['mix_col'], errors='coerce')
df.head()
output
我们来看一下各列的数据类型
df.dtypes
output
string_col float64
int_col int64
float_col float64
mix_col float64
missing_col float64
money_col object
boolean_col bool
custom object
dtype: object
float64
类型,要是我们想指定转换成我们想要的类型,例如df['mix_col'] = pd.to_numeric(df['mix_col'], errors='coerce').astype('Int64')
df['mix_col'].dtypes
output
Int64Dtype()
replace()
方法将这些符号给替换掉,然后再进行数据类型的转换df['money_replace'] = df['money_col'].str.replace('£', '').str.replace(',','')
df['money_replace'] = pd.to_numeric(df['money_replace'])
df['money_replace']
output
0 1000.0
1 2400.0
2 2400.0
3 2400.0
Name: money_replace, dtype: float64
regex=True
的参数,代码如下df['money_regex'] = df['money_col'].str.replace('[\£\,]', '', regex=True)
df['money_regex'] = pd.to_numeric(df['money_regex'])
df['money_regex']
astype()
方法,对多个列一步到位进行数据类型的转换,代码如下df = df.astype({
'string_col': 'float16',
'int_col': 'float16'
})
或者在第一步数据读取的时候就率先确定好数据类型,代码如下
df = pd.read_csv(
'dataset.csv',
dtype={
'string_col': 'float16',
'int_col': 'float16'
}
)
往期精彩回顾
适合初学者入门人工智能的路线及资料下载 中国大学慕课《机器学习》(黄海广主讲) 机器学习及深度学习笔记等资料打印 机器学习在线手册 深度学习笔记专辑 《统计学习方法》的代码复现专辑 AI基础下载 本站qq群955171419,加入微信群请扫码:
评论
真高!比亚迪员工爆料比亚迪在越南的薪资水平:基本工资480万,全勤奖35万,交通补助20万,餐补110万,每周6天,每天10小时
上一篇:某大公司为逼迫员工离职,竟然把他的工位安排到厕所旁,没想到他直接开始记录领导的如厕时间,还发到公司大群...对此,你怎么看?--完--PS:欢迎在留言区留下你的观点,一起讨论提高。如果今天的文章让你有新的启发,欢迎转发分享给更多人。全文完,感谢你的耐心阅读。如果你还想看到我的文章,请一定给本
开发者全社区
0
太敢穿了!透视纱裙!性感火辣的身材
绝了呀今天的厂花:吴宣仪1995年1月26日,吴宣仪出生于海南省海口市,中国内地流行乐女歌手、影视演员。2016年2月,吴宣仪随宇宙少女发行首张迷你专辑正式出道。2018年4月,她参加《创造101》综艺选秀,获得第二名,成功加入火箭少女101组合。吴宣仪的颜值一直备受称赞,她的五官立体精致,皮肤白皙
逆锋起笔
0
英伟达Blackwell平台网络配置分析
本文来自“英伟达Blachwell平台网络配置详解”。GTC大会英伟达展示了全新的 Blackwell 平台系列产品,包括 HGX B100 服务器、NVLINK Switch、GB200Superchip Computer Node、Quantum X800 交换机和 CX8 网卡(InfiniB
架构师技术联盟
0
某大公司为逼迫员工离职,竟然把他的工位安排到厕所旁,没想到他直接开始记录领导的如厕时间,还发到公司大群...
上一篇:字节的跳动职级与薪资(2024年)我们与公司间的合作,宛如两艘船只在茫茫大海上相互依靠,共同抵御风浪,携手驶向成功的彼岸。然而,当航向开始产生分歧,或是波涛汹涌的风浪改变了我们的初衷,我们或许应当冷静地选择和平分手,而非在风雨中硬撑。最近,一位网友的遭遇引起了广大职场人的关注和热议。这位网友
开发者全社区
0
金融研究 | 使用Python测量关键审计事项的「信息含量」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-01-13-information-content-of-critical-aud
大邓和他的Python
0
我看阿里的年终奖总算发了!
到4月底了,这两天看朋友圈,发现阿里的年终奖终于发了,问了问老同学,也从网上检索了不少信息,基本搞清楚了阿里今年的年终奖情况。近来来阿里一些集团对绩效等级做了较大的调整,以前的旧绩效系统中,绩效分为3.25、3.5、3.75、4和5五个等级,其中4和5是较高绩效等级,较少见。而且之前3.5绩效内部划
公子龙
0
CVPR 2024|大视觉模型的开山之作!无需任何语言数据即可打造大视觉模型
↑ 点击蓝字 关注极市平台作者丨科技猛兽编辑丨极市平台极市导读 本文提出一种序列建模 (sequential modeling) 的方法,不使用任何语言数据,训练大视觉模型。>>加入极市CV技术交流群,走在计算机视觉的最前沿本文目录1 序列建模打造大视觉模型(来自 U
极市平台
1
金融研究(更新) | 使用Python构建关键审计事项的「信息含量」
Tips: 公众号推送后内容只能更改一次,且只能改20字符。如果内容出问题,或者想更新内容, 只能重复推送。为了更好的阅读体验,建议阅读本文博客版, 链接地址https://textdata.cn/blog/2023-01-13-information-content-of-critical-aud
大邓和他的Python
0