模块学习—数据分析—pandas

Python环境:3.10.0,已安装第三方库 pandas


目录

一、pandas概述

二、从文件中读取数据(方法)

1. 读取csv文件

2. 读取txt文件(自定义数据形式)

3. 读取excel文件

4. 读取MySQL

三、pandas数据结构(DateFrame & Series)

1. Series

2.DataFrame

3. 从DataFrame中查询到Series

4. 从DataFrame中查询到DataFrame


一、pandas概述

Pandas主要读取表格类型(二维数据类型)的数据,然后进行分析:

数据类型 说明 pandas读取方法 参数说明
csv, tsv, txt 用逗号分隔、tab分割的纯文本文件 pandas.read_csv(参数1) 参数1,均为文件路径
excel 微软xls或xlsx文件 pandas.read_cexcel(参数1) -
mysql 关系型数据库表 pandas.read_sql(参数1) -

二、从文件中读取数据(方法)

1. 读取csv文件

1.1 文件变量fobj = read_csv(文件路径)

from pandas import *
fobj = read_csv('D://桌面//stock30.csv')

1.2 使用 文件变量fobj.head() 读取到前几行文件。值得注意的是,pandas会自动添加一列数据作为操作索引值,因此,第一列不是原文件包含的内容。

>>> fobj.head()

   Unnamed: 0  code              name  lasttrade
0           1   MMM                3M     160.09
1           2   AXP  American Express     134.46
2           3  AAPL             Apple     326.12
3           4    BA            Boeing     345.02
4           5   CAT       Caterpillar     140.47

1.3 查看数据形状,即数据行列,使用 fobj.shape 方法,以元组形式返回(行,列)。

>>> fobj.shape
(30, 4)

1.4 查看列名列表,fobj.columns

>>> fobj.columns
Index(['Unnamed: 0', 'code', 'name', 'lasttrade'], dtype='object')

1.5 查看索引列,fobj.index

>>> fobj.index
RangeIndex(start=0, stop=30, step=1)

1.6 查看每列数据的数据类型,fobj.dtypes

>>> fobj.dtypes
Unnamed: 0      int64
code           object
name           object
lasttrade     float64
dtype: object

2. 读取txt文件(自定义数据形式)

2.1 读取文件,fobj = read_csv()

>>> fobj = read_csv(
···    'D://桌面//01.txt',                    #文件路径
···    sep = '/',                             #数据分割符   
···    header = None,                         #文件标题行
···    names = ['date', 'member', 'number'])  #自定义列名

3. 读取excel文件

3.1 类似csv文件

4. 读取MySQL

后续补充

三、pandas数据结构(DateFrame & Series)

访问数据元素类似于字典,使用键(索引值index)来访问值。

1. Series

一维数据列表。

1.1 仅有列表即可生成最简单的Series;索引值默认为0~正无穷。

>>> s1 = Series([1, '2', 'a', '我是数据'])
>>> s1
0       1
1       2
2       a
3    我是数据
dtype: object

1.2 可自定义索引值。 

>>> s1 = Series([1, '2', 'a', '我是数据'], index = [1, '52', 'a', '我是索引值'])
>>> s1
1           1
52          2
a           a
我是索引值    我是数据
dtype: object

1.3 使用字典创建Series类型数据

>>> s1 = Series({1:25, '48':56, '我是索引':'我是数据'})
>>> s1
1         25
48        56
我是索引    我是数据
dtype: object

另外:

1. 只获取索引值:.index方法;只获取数据:.values方法。两者皆以列表形式返回。

2. 批量访问数据,可以使用如下方法

>>> s1[[1, '48']]
1     25
48    56
dtype: object

3. 访问单一数据的类型,会返回数据的具体类型;批量访问数据的类型,返回的仍然是,Series数据类型。

2.DataFrame

即为二维or多维数据列表

既有行索引index,又有列索引columns。可以看作是由Series组成的字典。

使用字典嵌套列表的方法创建,其键,即为列索引,行索引默认添加为自然数列。

tips:

1. 访问其每个数据类型(按照列索引访问), object.dtypes,会返回具体数据类型(包括object, int, float...)

2. 获取行索引,obj.index;获取列索引,obj.columns。

3. 列索引一般需要自定义,访问列索引时返回一个列表;行索引一般会默认为自然数列(返回元组,(start=, stop=, step=)),也可自定义(返回一个列表)。

4. 若想使用行索引来访问数据时,使用obj.loc[索引值]的方法来访问,返回一个Series对象,其索引值此时变为原来的列索引值。

3. 从DataFrame中查询到Series

一行或者一列DataFrame即为一个Series对象。

查询方法类似于使用字典的键来访问值的过程。

4. 从DataFrame中查询到DataFrame

访问时:1. 一般使用切片方法;

2. 类似于Series中批量访问数据时的语法。

>>> s2 = DataFrame({
···     'one':[1, 2, 3, 4, 5],
···     'two':[11, 12, 13, 14, 15],
···     'three':[21, 22, 23, 24, 25]})
>>> s2
   one  two  three
0    1   11     21
1    2   12     22
2    3   13     23
3    4   14     24
4    5   15     25
>>> s2['two']#使用单一列索引访问
0    11
1    12
2    13
3    14
4    15
Name: two, dtype: int64
>>> s2.loc[2:4]#批量使用行索引访问
   one  two  three
2    3   13     23
3    4   14     24
4    5   15     25

值得注意的是,列表的切片是不包含末尾值的,但是在这两个数据结构中使用的切片方法是包含末尾值的。 

猜你喜欢

转载自blog.csdn.net/m0_61357071/article/details/121527671