一起学习Python常用模块——pandas
itomcoil 2025-01-04 20:22 37 浏览
作者介绍
@王多鱼
百度的一名推荐算法攻城狮。
主要负责推荐的召回和排序模型的优化工作。
1 前言
Pandas 是Python的一个数据分析包,它是为了解决数据分析任务而创建的。Pandas 纳入了大量库和一些标准的数据模型,提供了高效地操作大型数据集所需的工具。Pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
2 数据结构
数据结构:
系列(Series)
数据框(DataFrame)
面板(Panel)
(多个series → 多个数据框 → 面板)。这些数据结构构建在Numpy数组之上,这意味着它们很快。
导入包
1 >>> import pandas as pd
2 >>> import numpy as np
系列
1 # 以列表定义
2 >>> s = pd.Series(['a', 'b', 'c', 'd'])
3 >>> s
4 0 a
5 1 b
6 2 c
7 3 d
8 dtype: object
9
10 # 以字典定义
11 >>> s = pd.Series({'a' : 0., 'b' : 1., 'c' : 2.})
12 >>> s
13 a 0.0
14 b 1.0
15 c 2.0
16 dtype: float64
数据框
数据框的数据存储格式如下:
1 # 以列表定义
2 >>> data = [['Alex',10], ['Bob',12], ['Clarke',13]]
3 >>> df = pd.DataFrame(data, columns=['Name', 'Age'])
4 >>> df
5 Name Age
6 0 Alex 10
7 1 Bob 12
8 2 Clarke 13
9
10 # 以字典定义
11 >>> data = {'Name':['Tom', 'Jack', 'Steve', 'Ricky'], 'Age':[28,34,29,42]}
12 >>> df = pd.DataFrame(data)
13 >>> df
14 Age Name
15 0 28 Tom
16 1 34 Jack
17 2 29 Steve
18 3 42 Ricky
数据索引:
●底层是由索引和值构成的多元组,(index1, [index2,index3,] value)。再由这些多元组组合出可视化的数据框。
●缺失值:数据框某个位置的所以没有对应的多元组,则会显示缺省值。
定义带索引的系列
1 >>> data = {'a' : 0., 'b' : 1., 'c' : 2.}
2 >>> s = pd.Series(data, index=['b','c','d','a'])
3 >>> s
4 b 1.0
5 c 2.0
6 d NaN
7 a 0.0
8 dtype: float64
3 数据输入/输出
方法一、直接定义
●pd.Series
●pd.DataFrame
方法二、读取器函数
读入:
●read_csv/read_table
●read_sql
●read_html
●read_json
读出:
●to_csv
4 基本功能
数据结构的属性
1 >>> df
2 Age Name
3 0 28 Tom
4 1 34 Jack
5 2 29 Steve
6 3 42 Ricky
7
8 >>> df.axes
9 [RangeIndex(start=0, stop=4, step=1), Index([u'Age', u'Name'], dtype='object')]
10
11 >>> df.dtypes
12 Age int64
13 Name object
14 dtype: object
15
16 >>> df.size
17 8
18
19 >>> df.values
20 array([[28, 'Tom'],
21 [34, 'Jack'],
22 [29, 'Steve'],
23 [42, 'Ricky']], dtype=object)
简单统计
1 >>> df.describe(include='all')
2 Age Name
3 count 4.000000 4
4 unique NaN 4
5 top NaN Tom
6 freq NaN 1
7 mean 33.250000 NaN
8 std 6.396614 NaN
9 min 28.000000 NaN
10 25% 28.750000 NaN
11 50% 31.500000 NaN
12 75% 36.000000 NaN
13 max 42.000000 NaN
5 选择数据
定位函数(多轴索引)
●loc():基于标签索引
●iloc():基于整数索引
定位函数格式
df.loc[ 行索引, 列索引]
行索引选择数据图示:
列索引选择图示:
●指定索引
1 >>> df = pd.DataFrame(np.random.randn(8, 4),
2 index = ['a','b','c','d','e','f','g','h'],
3
4 columns = ['A', 'B', 'C', 'D'])
5 >>> df
6 A B C D
7 a -0.484976 1.958562 -0.073555 0.524286
8 b 1.681393 1.041901 -0.109796 0.836486
9 c 0.352229 0.656365 0.590963 0.908981
10 d 1.325258 1.199558 0.953455 -0.192507
11 e 0.573300 -0.202530 -0.699603 1.504382
12 f -1.423372 -0.311816 0.680950 -1.619343
13 g 0.771233 -0.101350 -0.207373 1.242127
14 h 0.084874 -0.655007 -0.834754 0.072229
15
16
17 >>> df.loc['a', ['A', 'B']]
18 A -0.484976
19 B 1.958562
●区间索引
1 >>>>> df.loc[:, 'A']
2 a -0.484976
3 b 1.681393
4 c 0.352229
5 d 1.325258
6 e 0.573300
7 f -1.423372
8 g 0.771233
9 h 0.084874
10 Name: A, dtype: float64
11
12
13 >>> df.loc['a':'e','A':'C']
14 A B C
15 a -0.484976 1.958562 -0.073555
16 b 1.681393 1.041901 -0.109796
17 c 0.352229 0.656365 0.590963
18 d 1.325258 1.199558 0.953455
19 e 0.573300 -0.202530 -0.699603
●布尔值索引
1 >>> df.loc[df.A>0,]
2 A B C D
3 b 1.681393 1.041901 -0.109796 0.836486
4 c 0.352229 0.656365 0.590963 0.908981
5 d 1.325258 1.199558 0.953455 -0.192507
6 e 0.573300 -0.202530 -0.699603 1.504382
7 g 0.771233 -0.101350 -0.207373 1.242127
8 h 0.084874 -0.655007 -0.834754 0.072229
9
10 >>> df.loc[df.A.isna(), ]
11 Empty DataFrame
12 Columns: [A, B, C, D]
13 Index: []
6 操作数据
排序
●sort_index():按索引排序
●sort_values():按值排序
1 >>> unsorted_df =pd.DataFrame({'col1':[2,1,1,1],'col2':[1,3,2,4]})
2 >>> unsorted_df
3 col1 col2
4 0 2 1
5 1 1 3
6 2 1 2
7 3 1 4
8
9
10 # 按某列排序
11 >>> unsorted_df.sort_values('col1')
12 col1 col2
13 1 1 3
14 2 1 2
15 3 1 4
16 0 2 1
17
18
19 # 按多列排序
20 >>> unsorted_df.sort_values(['col1','col2'])
21 col1 col2
22 2 1 2
23 1 1 3
24 3 1 4
25 0 2 1
聚合
●分组聚合:groupby + agg
groupby函数的图示,用于聚合相同key的数据。
>>> ipl_data = {'Team': ['Riders', 'Riders', 'Devils', 'Devils', 'Kings',
'kings', 'Kings', 'Kings', 'Riders', 'Royals', 'Royals', 'Riders'],
'Rank': [1, 2, 2, 3, 3,4 ,1 ,1,2 , 4,1,2],
'Year': [2014,2015,2014,2015,2014,2015,2016,2017,2016,2014,2015,2017],
'Points':[876,789,863,673,741,812,756,788,694,701,804,690]}
>>> df = pd.DataFrame(ipl_data)
>>> df
Points Rank Team Year
0 876 1 Riders 2014
1 789 2 Riders 2015
2 863 2 Devils 2014
3 673 3 Devils 2015
4 741 3 Kings 2014
5 812 4 kings 2015
6 756 1 Kings 2016
7 788 1 Kings 2017
8 694 2 Riders 2016
9 701 4 Royals 2014
10 804 1 Royals 2015
11 690 2 Riders 2017
# 创建数据分组
>>> df.groupby(['Team','Year'])
<pandas.core.groupby.generic.DataFrameGroupBy object at 0x112f28c10>
# 查看分组
>>> df.groupby('Team').groups
{('Kings', 2014): Int64Index([4], dtype='int64'), ('Royals', 2014): Int64Index([9], dtype='int64'), ('Riders', 2014): Int64Index([0], dtype='int64'), ('Riders', 2015): Int64Index([1], dtype='int64'), ('Kings', 2016): Int64Index([6], dtype='int64'), ('Riders', 2016): Int64Index([8], dtype='int64'), ('Riders', 2017): Int64Index([11], dtype='int64'), ('Devils', 2014): Int64Index([2], dtype='int64'), ('Devils', 2015): Int64Index([3], dtype='int64'), ('kings', 2015): Int64Index([5], dtype='int64'), ('Royals', 2015): Int64Index([10], dtype='int64'), ('Kings', 2017): Int64Index([7], dtype='int64')}
# 查看其中一个分组
>>> df.groupby(['Team','Year']).get_group(('Kings',2014))
Points Rank Team Year
4 741 3 Kings 2014
# 最新年份的数据(分组排序)
>>> df.sort_values(['Team','Year'],ascending=False).groupby('Team').nth(0)
Points Rank Year
Team
Devils 673 3 2015
Kings 788 1 2017
Riders 690 2 2017
Royals 804 1 2015
kings 812 4 2015
# 聚合函数
>>> df.groupby(['Year'])['Points'].agg('mean')
Year
2014 795.25
2015 769.50
2016 725.00
2017 739.00
Name: Points, dtype: float64
>>> df.groupby(['Year'])['Points'].agg(['mean','sum','median'])
mean sum median
Year
2014 795.25 3181 802.0
2015 769.50 3078 796.5
2016 725.00 1450 725.0
2017 739.00 1478 739.0
# 过滤筛选
>>> df.groupby('Team').filter(lambda x: len(x) >= 3)
Points Rank Team Year
0 876 1 Riders 2014
1 789 2 Riders 2015
4 741 3 Kings 2014
6 756 1 Kings 2016
7 788 1 Kings 2017
8 694 2 Riders 2016
11 690 2 Riders 2017
>>> df.groupby('Team').filter(lambda x:max(x['Points'])>=800)
Points Rank Team Year
0 876 1 Riders 2014
1 789 2 Riders 2015
2 863 2 Devils 2014
3 673 3 Devils 2015
5 812 4 kings 2015
8 694 2 Riders 2016
9 701 4 Royals 2014
10 804 1 Royals 2015
11 690 2 Riders 2017
●窗口聚合:rolling + agg
做定量模型比较常用。
应用函数
●pipe():表格应用函数,应用于整个表格,方便链式编程
>>> def adder(x,y):
return x+y
>>> df = pd.DataFrame(np.random.randn(5,3), columns=['col1','col2','col3'])
>>> df
col1 col2 col3
0 1.200842 -0.387094 0.218903
1 -2.469144 2.283831 0.342451
2 0.688127 0.445456 0.966626
3 0.912838 0.577441 -0.967456
4 -0.706913 0.791318 -1.040644
>>> df.pipe(adder, 2)
col1 col2 col3
0 3.200842 1.612906 2.218903
1 -0.469144 4.283831 2.342451
2 2.688127 2.445456 2.966626
3 2.912838 2.577441 1.032544
4 1.293087 2.791318 0.959356
●apply():行列应用函数
>>> df.apply(np.mean)
col1 -0.074850
col2 0.742191
col3 -0.096024
dtype: float64
>>> df.apply(np.mean,axis=1)
0 0.344217
1 0.052380
2 0.700070
3 0.174274
4 -0.318746
dtype: float64
●applymap():元素映射函数,类似于map()
>>> aes_encrypt = crypto_util.AesEncrypt()
>>> def decrypt(line):
decrypt_str = aes_encrypt.decrypt(line,
crypto_util.constants.Constants.CRM_ENCRYPT_PREFIX)
return decrypt_str
>>> df = pd.DataFrame(
['baiducrmcommonciper_LUjEqeTBXHcHFak5E3lwcgOR+Xfl6v/hkbSrzqBBFI4=',
'baiducrmcommonciper_4TReevfj06k3mg8871PvslHvPuPwlCUkn4xM6ZjrAn4=',
'baiducrmcommonciper_zmrudGYBOalk5LTqlF5ncg=='])
>>> df.applymap(decrypt)
0
0 25339384668@qq.com
1 1909062174@qq.com
2 8076719440@qq.om
7 操作数据框
连结
●append
●concat
concat 函数功能如下图所示,(1)不指定axis时,默认axis=0,上下拼接;(2)指定axis=1时,左右拼接。
>>> one = pd.DataFrame({
'Name': ['Alex', 'Amy', 'Allen', 'Alice', 'Ayoung'],
'subject_id':['sub1','sub2','sub4','sub6','sub5'],
'Marks_scored':[98,90,87,69,78]},
index=[1,2,3,4,5])
>>> two = pd.DataFrame({
'Name': ['Billy', 'Brian', 'Bran', 'Bryce', 'Betty'],
'subject_id':['sub2','sub4','sub3','sub6','sub5'],
'Marks_scored':[89,80,79,97,88]},
index=[1,2,3,4,5])
>>> pd.concat([one, two])
Marks_scored Name subject_id
1 98 Alex sub1
2 90 Amy sub2
3 87 Allen sub4
4 69 Alice sub6
5 78 Ayoung sub5
1 89 Billy sub2
2 80 Brian sub4
3 79 Bran sub3
4 97 Bryce sub6
5 88 Betty sub5
>>> pd.concat([one, two], axis = 1)
Marks_scored Name subject_id Marks_scored Name subject_id
1 98 Alex sub1 89 Billy sub2
2 90 Amy sub2 80 Brian sub4
3 87 Allen sub4 79 Bran sub3
4 69 Alice sub6 97 Bryce sub6
5 78 Ayoung sub5 88 Betty sub5
Merge
pd.merge(left,right,how='inner',on=None, left_on=None, right_on=None,left_index=False,right_index=False,sort=True)
merge函数图示:
8 画图
%matplotlib inline
import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
plt.rc('figure', figsize=(5, 3))
ts = pd.Series(np.random.randn(1000),
index=pd.date_range('1/1/2000', periods=1000))
ts = ts.cumsum()
ts.plot()
df = pd.DataFrame(np.random.randn(1000, 4),
index = ts.index,
columns=list('ABCD'))
df = df.cumsum()
plt.figure(); df.plot(); plt.legend(loc='best')
大家尽情的用Pandas玩耍数据吧 ~
相关推荐
- selenium(WEB自动化工具)
-
定义解释Selenium是一个用于Web应用程序测试的工具。Selenium测试直接运行在浏览器中,就像真正的用户在操作一样。支持的浏览器包括IE(7,8,9,10,11),MozillaF...
- 开发利器丨如何使用ELK设计微服务中的日志收集方案?
-
【摘要】微服务各个组件的相关实践会涉及到工具,本文将会介绍微服务日常开发的一些利器,这些工具帮助我们构建更加健壮的微服务系统,并帮助排查解决微服务系统中的问题与性能瓶颈等。我们将重点介绍微服务架构中...
- 高并发系统设计:应对每秒数万QPS的架构策略
-
当面试官问及"如何应对每秒几万QPS(QueriesPerSecond)"时,大概率是想知道你对高并发系统设计的理解有多少。本文将深入探讨从基础设施到应用层面的解决方案。01、理解...
- 2025 年每个 JavaScript 开发者都应该了解的功能
-
大家好,很高兴又见面了,我是"高级前端进阶",由我带着大家一起关注前端前沿、深入前端底层技术,大家一起进步,也欢迎大家关注、点赞、收藏、转发。1.Iteratorhelpers开发者...
- JavaScript Array 对象
-
Array对象Array对象用于在变量中存储多个值:varcars=["Saab","Volvo","BMW"];第一个数组元素的索引值为0,第二个索引值为1,以此类推。更多有...
- Gemini 2.5编程全球霸榜,谷歌重回AI王座,神秘模型曝光,奥特曼迎战
-
刚刚,Gemini2.5Pro编程登顶,6美元性价比碾压Claude3.7Sonnet。不仅如此,谷歌还暗藏着更强的编程模型Dragontail,这次是要彻底翻盘了。谷歌,彻底打了一场漂亮的翻...
- 动力节点最新JavaScript教程(高级篇),深入学习JavaScript
-
JavaScript是一种运行在浏览器中的解释型编程语言,它的解释器被称为JavaScript引擎,是浏览器的一部分,JavaScript广泛用于浏览器客户端编程,通常JavaScript脚本是通过嵌...
- 一文看懂Kiro,其 Spec工作流秒杀Cursor,可移植至Claude Code
-
当Cursor的“即兴编程”开始拖累项目质量,AWS新晋IDEKiro以Spec工作流打出“先规范后编码”的系统工程思维:需求-设计-任务三件套一次生成,文档与代码同步落地,复杂项目不...
- 「晚安·好梦」努力只能及格,拼命才能优秀
-
欢迎光临,浏览之前点击上面的音乐放松一下心情吧!喜欢的话给小编一个关注呀!Effortscanonlypass,anddesperatelycanbeexcellent.努力只能及格...
- JavaScript 中 some 与 every 方法的区别是什么?
-
大家好,很高兴又见面了,我是姜茶的编程笔记,我们一起学习前端相关领域技术,共同进步,也欢迎大家关注、点赞、收藏、转发,您的支持是我不断创作的动力在JavaScript中,Array.protot...
- 10个高效的Python爬虫框架,你用过几个?
-
小型爬虫需求,requests库+bs4库就能解决;大型爬虫数据,尤其涉及异步抓取、内容管理及后续扩展等功能时,就需要用到爬虫框架了。下面介绍了10个爬虫框架,大家可以学习使用!1.Scrapysc...
- 12个高效的Python爬虫框架,你用过几个?
-
实现爬虫技术的编程环境有很多种,Java、Python、C++等都可以用来爬虫。但很多人选择Python来写爬虫,为什么呢?因为Python确实很适合做爬虫,丰富的第三方库十分强大,简单几行代码便可实...
- pip3 install pyspider报错问题解决
-
运行如下命令报错:>>>pip3installpyspider观察上面的报错问题,需要安装pycurl。是到这个网址:http://www.lfd.uci.edu/~gohlke...
- PySpider框架的使用
-
PysiderPysider是一个国人用Python编写的、带有强大的WebUI的网络爬虫系统,它支持多种数据库、任务监控、项目管理、结果查看、URL去重等强大的功能。安装pip3inst...
- 「机器学习」神经网络的激活函数、并通过python实现激活函数
-
神经网络的激活函数、并通过python实现whatis激活函数感知机的网络结构如下:左图中,偏置b没有被画出来,如果要表示出b,可以像右图那样做。用数学式来表示感知机:上面这个数学式子可以被改写:...
- 一周热门
- 最近发表
- 标签列表
-
- ps图案在哪里 (33)
- super().__init__ (33)
- python 获取日期 (34)
- 0xa (36)
- super().__init__()详解 (33)
- python安装包在哪里找 (33)
- linux查看python版本信息 (35)
- python怎么改成中文 (35)
- php文件怎么在浏览器运行 (33)
- eval在python中的意思 (33)
- python安装opencv库 (35)
- python div (34)
- sticky css (33)
- python中random.randint()函数 (34)
- python去掉字符串中的指定字符 (33)
- python入门经典100题 (34)
- anaconda安装路径 (34)
- yield和return的区别 (33)
- 1到10的阶乘之和是多少 (35)
- python安装sklearn库 (33)
- dom和bom区别 (33)
- js 替换指定位置的字符 (33)
- python判断元素是否存在 (33)
- sorted key (33)
- shutil.copy() (33)