Pandas统计分析基础(1):Pandas的安装与Pandas引入的新数据类型:Series与 DataFrame基本

✅作者简介:大家好我是Xlong,一枚正在学习COMSOL、Python的工科研究僧
个人主页:  Xlong的个人博客主页
系列专栏:  Python大数据分析
如果觉得博主的文章还不错的话,请支持一下博主哦

目录

一、前言

二、安装

2、Pandas引入的新数据类型:Series与 DataFrame

2.1 Series(一维数据)

(1)数组

(2)字典

(3)标量值

2.2 DataFrame(二维数据)

(1)用 Series 字典生成 DataFrame

(2)用多维数组字典、列表字典生成 DataFrame

三、参考资料


一、前言

        Pandas 是 Python 的核心数据分析支持库,提供了快速、灵活、明确的数据结构,旨在简单、直观地处理关系型、标记型数据。

        Pandas 适用于处理以下类型的数据:

  1. 与 SQL 或 Excel 表类似的,含异构列的表格数据;
  2. 有序和无序(非固定频率)的时间序列数据;
  3. 带行列标签的矩阵数据,包括同构或异构型数据;
  4. 任意其它形式的观测、统计数据集, 数据转入 Pandas 数据结构时不必事先标记。

        Pandas 的主要数据结构是 Series(一维数据) DataFrame(二维数据),这两种数据结构足以处理金融、统计、社会科学、工程等领域里的大多数典型用例。对于 R 用户,DataFrame 提供了比 R 语言 data.frame 更丰富的功能。Pandas 基于 NumPy 开发,可以与其它第三方科学计算支持库完美集成。

二、安装

        1.安装Pandas的最简单方法是将其安装为Anaconda发行版的一部分,这是一种用于数据分析和科学计算的跨平台发行版。这是大多数用户的推荐安装方法。

        2.pip install Pandas

        3.如果是用的Pycharm开发平台,那就需要依次点击“文件”→“Python Interpreter”→“+”→“搜索pandas”→Install Package,如下图所示:Pandas统计分析基础(1):Pandas的安装与Pandas引入的新数据类型:Series与 DataFrame基本_第1张图片

需要注意:

  • pandas新版已经不再支持python2
  • pandas依赖numpy
import pandas as pd
print(pd.__version__) #看看自己的版本,过低的话可能会有些功能不支持
#运行结果:1.4.1

如果像上面演示的这样,会出现pandas的版本,也就说明安装成功了!

import numpy as np
import matplotlib.pyplot as plt
plt.rcParams['font.sans-serif']=['SimHei']  #图中字体改为黑体以兼容中文
plt.rcParams['axes.unicode_minus']=False #负号显示的问题

2、Pandas引入的新数据类型:Series与 DataFrame

2.1 Series(一维数据)

        Series 是带标签的一维数组,可存储整数、浮点数、字符串、Python 对象等类型的数据。轴标签统称为索引。调用 pd.Series 函数即可创建 Series:

        s = pd.Series(data, index=index)

上述代码中,data 支持以下数据类型:

  • Python 字典
  • 数组
  • 标量值(如,5)

        index 是轴标签列表。不同数据可分为以下几种情况:

(1)数组

        data 是数组时,index 长度必须与 data 长度一致。没有指定 index 参数时,创建数值型索引,即 [0, ..., len(data) - 1]。

import numpy as np
import pandas as pd
s = pd.Series(np.random.randn(5))
print(s)

运行结果:

0    1.634802
1   -0.847340
2    0.813301
3    1.017809
4    0.294289
dtype: float64

import numpy as np
import pandas as pd
s = pd.Series(np.random.randn(5),index=['a', 'b', 'c', 'd', 'e'])
print(s)
print(s.index) #可以通过.index调用一个Series的index

运行结果:

a   -0.101459
b    0.708805
c    0.757455
d    0.360905
e    0.920713
dtype: float64
Index(['a', 'b', 'c', 'd', 'e'], dtype='object')

(2)字典

        Series 可以用字典实例化:

import pandas as pd
dic = {'b': 1, 'a': 0, 'c': 2}
print(pd.Series(dic))

运行结果:

b    1
a    0
c    2
dtype: int64

(3)标量值

        data 是标量值时,必须提供索引。Series 按索引长度重复该标量值。

import pandas as pd
A=pd.Series(5., index=['a', 'b', 'c', 'd', 'e'])
print(A)

运行结果:

a    5.0
b    5.0
c    5.0
d    5.0
e    5.0
dtype: float64 

2.2 DataFrame(二维数据)

        DataFrame 是由多种类型的列构成的二维标签数据结构,类似于 Excel 、SQL 表,或 Series 对象构成的字典。DataFrame 是最常用的 Pandas 对象,与 Series 一样,DataFrame 支持多种类型的输入数据:

  • 一维 ndarray、列表、字典、Series 字典
  • 二维 numpy.ndarray
  • Series
  • DataFrame

        除了数据,还可以有选择地传递 index(行标签)和 columns(列标签)参数。传递了索引或列,就可以确保生成的 DataFrame 里包含索引或列。Series 字典加上指定索引时,会丢弃与传递的索引不匹配的所有数据。

        没有传递轴标签时,按常规依据输入数据进行构建。

(1)用 Series 字典生成 DataFrame

        生成的索引是每个 Series 索引的并集。先把嵌套字典转换为 Series。如果没有指定列,DataFrame 的列就是字典键的有序列表。

import pandas as pd
d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
     'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
print(d,'\n')
df = pd.DataFrame(d) #大小写敏感
print(df,'\n')
di = pd.DataFrame(d, index=['d', 'b', 'a'])
print(di)

运行结果:

{'one': a    1.0
b    2.0
c    3.0
dtype: float64, 'two': a    1.0
b    2.0
c    3.0
d    4.0
dtype: float64}

   one  two
a  1.0  1.0
b  2.0  2.0
c  3.0  3.0
d  NaN  4.0

   one  two
d  NaN  4.0
b  2.0  2.0
a  1.0  1.0

        通过字典组成dataframe时,index一样的serie会共用index,否则会将不同的index拼接起来。

import pandas as pd
d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
     'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
A = pd.DataFrame(d, index=['d', 'b', 'a','c'], columns=['two', 'three'])
print(A)

运行结果:

   two three
d  4.0   NaN
b  2.0   NaN
a  1.0   NaN
c  3.0   NaN

        index 和 columns 属性分别用于访问行、列标签:

import pandas as pd
d = {'one': pd.Series([1., 2., 3.], index=['a', 'b', 'c']),
     'two': pd.Series([1., 2., 3., 4.], index=['a', 'b', 'c', 'd'])}
A = pd.DataFrame(d, index=['d', 'b', 'a','c'], columns=['two', 'three'])
print(A.index)
print(A.columns)

运行结果:

Index(['d', 'b', 'a', 'c'], dtype='object')
Index(['two', 'three'], dtype='object')

(2)用多维数组字典、列表字典生成 DataFrame

        多维数组的长度必须相同。如果传递了索引参数,index 的长度必须与数组一致。如果没有传递索引参数,生成的结果是 range(n),n 为数组长度。

import pandas as pd
d = {'one': [1., 2., 3., 4.],'two': [4., 3., 2., 1.]}
print(d,'\n')
A = pd.DataFrame(d)
print(A)

运行结果:

{'one': [1.0, 2.0, 3.0, 4.0], 'two': [4.0, 3.0, 2.0, 1.0]}

   one  two
0  1.0  4.0
1  2.0  3.0
2  3.0  2.0
3  4.0  1.0

import pandas as pd
d = {'one': [1., 2., 3., 4.],'two': [4., 3., 2., 1.]}
A = pd.DataFrame(d)
print(A,'\n')
A.index=['a', 'b', 'c', 'd']
print(A)

运行结果:

   one  two
0  1.0  4.0
1  2.0  3.0
2  3.0  2.0
3  4.0  1.0

   one  two
a  1.0  4.0
b  2.0  3.0
c  3.0  2.0
d  4.0  1.0

import pandas as pd
d = {'one': [1., 2., 3., 4.],'two': [4., 3., 2., 1.]}
A = pd.DataFrame(d)
print(A,'\n')
A.index=['a', 'b', 'c', 'd']
print(A)
A = pd.DataFrame(d, index=['a', 'b', 'c', 'd'])
print(A)

运行结果:

    one  two
0  1.0  4.0
1  2.0  3.0
2  3.0  2.0
3  4.0  1.0

   one  two
a  1.0  4.0
b  2.0  3.0
c  3.0  2.0
d  4.0  1.0
   one  two
a  1.0  4.0
b  2.0  3.0
c  3.0  2.0
d  4.0  1.0

三、参考资料

菜鸟教程:Pandas教程https://www.runoob.com/pandas/pandas-tutorial.html

         老师课件

        以上就是Pandas统计分析基础(1)之Pandas的安装与Pandas引入的新数据类型:Series与 DataFrame基本,如果有改进的建议,欢迎在评论区留言交流~

        持续更新中......原创不易,各位看官请随手点下Follow和Star,感谢!!!

你可能感兴趣的:(Python大数据分析,python,机器学习,开发语言,pycharm)