百度360必应搜狗淘宝本站头条
当前位置:网站首页 > 技术文章 > 正文

一起学习Python常用模块——pandas

itomcoil 2025-01-04 20:22 29 浏览

作者介绍

@王多鱼

百度的一名推荐算法攻城狮。

主要负责推荐的召回和排序模型的优化工作。


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玩耍数据吧 ~

相关推荐

Python Qt GUI设计:将UI文件转换Python文件三种妙招(基础篇—2)

在开始本文之前提醒各位朋友,Python记得安装PyQt5库文件,Python语言功能很强,但是Python自带的GUI开发库Tkinter功能很弱,难以开发出专业的GUI。好在Python语言的开放...

Connect 2.0来了,还有Nuke和Maya新集成

ftrackConnect2.0现在可以下载了--重新设计的桌面应用程序,使用户能够将ftrackStudio与创意应用程序集成,发布资产等。这个新版本的发布中还有两个Nuke和Maya新集成,...

Magicgui:不会GUI编程也能轻松构建Python GUI应用

什么是MagicguiMagicgui是一个Python库,它允许开发者仅凭简单的类型注解就能快速构建图形用户界面(GUI)应用程序。这个库基于Napari项目,利用了Python的强大类型系统,使得...

Python入坑系列:桌面GUI开发之Pyside6

阅读本章之后,你可以掌握这些内容:Pyside6的SignalsandSlots、Envents的作用,如何使用?PySide6的Window、DialogsandAlerts、Widgets...

Python入坑系列-一起认识Pyside6 designer可拖拽桌面GUI

通过本文章,你可以了解一下内容:如何安装和使用Pyside6designerdesigner有哪些的特性通过designer如何转成python代码以前以为Pyside6designer需要在下载...

pyside2的基础界面(pyside2显示图片)

今天我们来学习pyside2的基础界面没有安装过pyside2的小伙伴可以看主页代码效果...

Python GUI开发:打包PySide2应用(python 打包pyc)

之前的文章我们介绍了怎么使用PySide2来开发一个简单PythonGUI应用。这次我们来将上次完成的代码打包。我们使用pyinstaller。注意,pyinstaller默认会将所有安装的pack...

使用PySide2做窗体,到底是怎么个事?看这个能不能搞懂

PySide2是Qt框架的Python绑定,允许你使用Python创建功能强大的跨平台GUI应用程序。PySide2的基本使用方法:安装PySide2pipinstallPy...

pycharm中conda解释器无法配置(pycharm安装的解释器不能用)

之前用的好好的pycharm正常配置解释器突然不能用了?可以显示有这个环境然后确认后可以conda正在配置解释器,但是进度条结束后还是不成功!!试过了pycharm重启,pycharm重装,anaco...

Conda使用指南:从基础操作到Llama-Factory大模型微调环境搭建

Conda虚拟环境在Linux下的全面使用指南:从基础操作到Llama-Factory大模型微调环境搭建在当今的AI开发与数据分析领域,conda虚拟环境已成为Linux系统下管理项目依赖的标配工具。...

Python操作系统资源管理与监控(python调用资源管理器)

在现代计算环境中,对操作系统资源的有效管理和监控是确保应用程序性能和系统稳定性的关键。Python凭借其丰富的标准库和第三方扩展,提供了强大的工具来实现这一目标。本文将探讨Python在操作系统资源管...

本地部署开源版Manus+DeepSeek创建自己的AI智能体

1、下载安装Anaconda,设置conda环境变量,并使用conda创建python3.12虚拟环境。2、从OpenManus仓库下载代码,并安装需要的依赖。3、使用Ollama加载本地DeepSe...

一文教会你,搭建AI模型训练与微调环境,包学会的!

一、硬件要求显卡配置:需要Nvidia显卡,至少配备8G显存,且专用显存与共享显存之和需大于20G。二、环境搭建步骤1.设置文件存储路径非系统盘存储:建议将非安装版的环境文件均存放在非系统盘(如E盘...

使用scikit-learn为PyTorch 模型进行超参数网格搜索

scikit-learn是Python中最好的机器学习库,而PyTorch又为我们构建模型提供了方便的操作,能否将它们的优点整合起来呢?在本文中,我们将介绍如何使用scikit-learn中的网格搜...

如何Keras自动编码器给极端罕见事件分类

全文共7940字,预计学习时长30分钟或更长本文将以一家造纸厂的生产为例,介绍如何使用自动编码器构建罕见事件分类器。现实生活中罕见事件的数据集:背景1.什么是极端罕见事件?在罕见事件问题中,数据集是...