Pandas 简介


一、Pandas 是什么?

Python 在数据处理上独步天下:代码灵活、开发快速;尤其是 Python 的 Pandas 包,无论是在数据分析领域、还是大数据开发场景,都具有显著的优势;

  1. Pandas 是 Python 的一个第三方包,是基于NumPy 的一种工具,也是商业和工程领域最流行的结构化数据工具集,用于数据清洗、处理以及分析;
  2. Pandas 和 Spark有很多功能都类似,甚至使用方法都是相同的;当我们学会 Pandas 之后,再学习 Spark 就更加简单快速
  3. Pandas 在整个数据开发的流程中的应用场景:在大数据场景下,数据在流转的过程中,Python Pandas 丰富的 API 能够更加灵活、快速地对数据进行清洗和处理
  4. Pandas 在数据处理上具有独特的优势:
    (1)底层是基于 Numpy 构建的,所以运行速度特别的快
    (2)有专门的处理缺失数据的 API
    (3)强大而灵活的分组、聚合、转换功能

二、安装 pandas 的库

windows上打开 cmd 界面,或者在 mac 平台打开终端 termial 界面,执行下面命令即可完成安装。

pip install -i https://pypi.tuna.tsinghua.edu.cn/simple/ pandas

解释:
(1)https://pypi.tuna.tsinghua.edu.cn/simple/ 此地址为清华镜像的地址,在国内下载 python 相关包的时候建议使用此地址;
(2)mac平台系统自带pip3工具,可以在“/Users/用户名xxx/Library/Python/3.8/bin”目录下找到,如果安装pandas时 终端提示找不到 pip3 或者 pip 命令,可以将 “/Users/用户名xxx/Library/Python/3.8/bin” 添加到环境变量中。路径中的用户名需改成与自己mac系统对应的用户名即可。
添加环境变量的步骤如下:
(1)在 Home 目录下创建 .bash_profile文件(如果有,则直接打开),命令如下:

cd ~
vim .bash_profile

(2)在.bash_profile文件内容的最后一行追加以下内容,注意,用户名需修改成与自己mac系统对应的用户名

#PIP_HOME
PATH="/Users/与自己mac系统对应的用户名xxx/Library/Python/3.8/bin:${PATH}"
export PATH

(3)执行source命令,让环境变量生效,如下:

source ~/.bash_profile

这样,你就可以在终端使用 pip 或者 pip3 命令了。

最后,下面以mac平台为例,安装过程及相关日志如下所示:

a1@1deMac-mini ~ % pip install -i https://pypi.tuna.tsinghua.edu.cn/simple pandas
Defaulting to user installation because normal site-packages is not writeable
Looking in indexes: https://pypi.tuna.tsinghua.edu.cn/simple
Collecting pandas
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/c8/85/8afe540bd0299c4d58f0a5b88acc49a8021804abe05a00d2cbc2fccde873/pandas-1.4.3-cp38-cp38-macosx_10_9_x86_64.whl (11.4 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 11.4/11.4 MB 1.7 MB/s eta 0:00:00
Collecting pytz>=2020.1
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/d5/50/54451e88e3da4616286029a3a17fc377de817f66a0f50e1faaee90161724/pytz-2022.2.1-py2.py3-none-any.whl (500 kB)
Collecting python-dateutil>=2.8.1
  Using cached https://pypi.tuna.tsinghua.edu.cn/packages/36/7a/87837f39d0296e723bb9b62bbb257d0355c7f6128853c78955f57342a56d/python_dateutil-2.8.2-py2.py3-none-any.whl (247 kB)
Collecting numpy>=1.18.5
  Downloading https://pypi.tuna.tsinghua.edu.cn/packages/fc/90/fa2ca0f2fcabbfd970e1e78f820d8639683c36525e1c89d9bd20e69230a7/numpy-1.23.2-cp38-cp38-macosx_10_9_x86_64.whl (18.1 MB)
     ━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━━ 18.1/18.1 MB 2.6 MB/s eta 0:00:00
Requirement already satisfied: six>=1.5 in /Applications/Xcode.app/Contents/Developer/Library/Frameworks/Python3.framework/Versions/3.8/lib/python3.8/site-packages (from python-dateutil>=2.8.1->pandas) (1.15.0)
Installing collected packages: pytz, python-dateutil, numpy, pandas
  WARNING: The scripts f2py, f2py3 and f2py3.8 are installed in '/Users/a1/Library/Python/3.8/bin' which is not on PATH.
  Consider adding this directory to PATH or, if you prefer to suppress this warning, use --no-warn-script-location.
Successfully installed numpy-1.23.2 pandas-1.4.3 python-dateutil-2.8.2 pytz-2022.2.1
a1@1deMac-mini ~ % 

终端出现 Successfully installed 即为安装成功。

三、使用步骤

1.引入库

代码如下(示例):

import numpy as np
import pandas as pd
import matplotlib.pyplot as plt
import seaborn as sns
import warnings
warnings.filterwarnings('ignore')
import  ssl
ssl._create_default_https_context = ssl._create_unverified_context

2.读入数据

代码如下(示例):

data = pd.read_csv(
    'https://labfile.oss.aliyuncs.com/courses/1283/adult.data.csv')
print(data.head())

总结

以上就是今天要讲的内容,本文仅仅简单介绍了pandas的使用,而pandas提供了大量能使我们快速便捷地处理数据的函数和方法。
链接:https://www.jianshu.com/p/5881f636b2d2
来源:简书

  • 0
    点赞
  • 3
    收藏
    觉得还不错? 一键收藏
  • 0
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值