Python数据分析之Pandas(一)
·
一、Pandas简介
Pandas 是 Python 语言的一个扩展程序库,用于数据分析。
Pandas 是一个开放源码、BSD 许可的库,提供高性能、易于使用的数据结构和数据分析工具。
Pandas 一个强大的分析结构化数据的工具集,基础是 Numpy(提供高性能的矩阵运算)。
Pandas 可以从各种文件格式比如 CSV、JSON、SQL、Microsoft Excel 导入数据。
Pandas 可以对各种数据进行运算操作,比如归并、再成形、选择,还有数据清洗和数据加工特征。
Pandas 广泛应用在学术、金融、统计学等各个数据分析领域。
Pandas 的主要数据结构是 Series (一维数据)与 DataFrame(二维数据),这两种数据结构足以处理金融、统计、社会科学、工程等领域里的大多数典型用例。
二、pandas简单运用
#导库
import pandas as pd
import numpy as np
1、定义DataFrame
#DataFrame
df_DataFrame = pd.DataFrame(np.random.randn(3,4))
print(df_DataFrame)
0 1 2 3
0 1.714211 -0.092753 -1.686740 1.580697
1 0.758850 -0.525707 2.732060 -0.888418
2 1.880706 0.977837 0.298658 -2.558101
2、定义Series
#Series
df_Series = pd.Series(np.arange(12))
print(df_Series)
0 0
1 1
2 2
3 3
4 4
5 5
6 6
7 7
8 8
9 9
10 10
11 11
dtype: int64
3、转置
#转置
df_DataFrame.T
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-xVFo2IX6-1624608281841)(/Users/dc/程序员唐丁/唐丁博客/配图/转置.png)]](https://i-blog.csdnimg.cn/blog_migrate/9dfe6f487ec4565544019cfdba174d84.png)
4、排序-按行索引
#排序-按行索引
df_DataFrame = pd.DataFrame(np.random.randn(3,4),index=['a','b','c'],columns=['A','B','C','D'])
df_DataFrame.sort_index(axis=0,ascending=False)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-kZj8JVDp-1624608281847)(/Users/dc/程序员唐丁/唐丁博客/配图/排序-按行索引.png)]](https://i-blog.csdnimg.cn/blog_migrate/2b1553af1913e2ca352a01679e60ecac.png)
5、排序-按列索引
#排序-按列索引
df_DataFrame = pd.DataFrame(np.random.randn(3,4),index=['a','b','c'],columns=['A','B','C','D'])
df_DataFrame.sort_index(axis=1,ascending=False)
![[外链图片转存失败,源站可能有防盗链机制,建议将图片保存下来直接上传(img-JOcy7IEA-1624608281849)(/Users/dc/程序员唐丁/唐丁博客/配图/排序-按列索引.png)]](https://i-blog.csdnimg.cn/blog_migrate/aea08e4bf4293af29ebd665d57a5e1ef.png)
6、排序-根据值排序
#排序-根据值排序df_DataFrame = pd.DataFrame(np.random.randn(3,4),index=['a','b','c'],columns=['A','B','C','D'])df_DataFrame.sort_values(by='B')


魔乐社区(Modelers.cn) 是一个中立、公益的人工智能社区,提供人工智能工具、模型、数据的托管、展示与应用协同服务,为人工智能开发及爱好者搭建开放的学习交流平台。社区通过理事会方式运作,由全产业链共同建设、共同运营、共同享有,推动国产AI生态繁荣发展。
更多推荐


所有评论(0)