Pandas基础(一)

Pandas是什么?

Pandas是数据分析的核心工具包,基于Numpy创建,为数据分析而存在。

  • 一维数组Series + 二维数组Dataframe
  • 可直接读取数据并做处理(高效简单)
  • 兼容各种数据库
  • 支持各种分析方法

    Pandas基本数据结构-Series的基本概念

    先举个栗子:
    1
    2
    3
    4
    5
    6
    7
    8
    9
    10
    11
    12
    ar = np.random.rand(5)
    s = pd.Series(ar)
    print(ar)
    print(s,type(s))
    >>>
    [0.1206055 0.83147658 0.88649587 0.2162775 0.31466148]
    0 0.120605
    1 0.831477
    2 0.886496
    3 0.216277
    4 0.314661
    dtype: float64 <class 'pandas.core.series.Series'>

在这里可以看到这里的Series相比与之前学习的ndarray是一个自带索引index的数组 = 一维的数组 + 对应的索引,当pd.Series单单只看values时就是一个ndarray。

在具体操作方面,Series和ndarray基本相似,包括索引切片的操作差别并不大。

Pandas基本数据结构-Series的创建方法

字典创建Series
1
2
3
4
# 字典创建Series
dic = {'a':1, 'b':2, 'c':3}
s = pd.Series(dic)
print(s)
数组创建Series
1
2
3
4
5
# 数组创建Series
arr = np.random.rand(5)*200
s = pd.Series(arr)
print(arr)
print(s,type(s))
Series的参数设置

我们可以通过指定Series的index以及dtype参数创建符合我们要求的Series。

1
2
3
4
5
6
# Series的参数设置
s = pd.Series(arr,index=list('abcde'),dtype=np.int,name=test)
print(s)
# index参数:设置index,长度保持一致
# dtype参数:设置数值类型
# 那么参数:设置名称

通过标量创建Series
1
2
s = pd.Series(arr,index=np.arange(5))
print(s)

Pandas基本数据结构-Series的索引

位置下标索引

位置下标从0开始,索引结果为numpy.float格式并且可以通过float()格式转换为float格式,且位置下标索引是没有负数的。

1
2
3
4
5
6
7
8
9
10
11
12
13
s = pd.Series(np.random.rand(5))
print(s)
print(s[0],type(s[0]),s[0].dtype)
print(float(s[0]),type(float(s[0])))
>>>
0 0.495361
1 0.152195
2 0.217591
3 0.748394
4 0.093389
dtype: float64
0.49536125725281055 <class 'numpy.float64'> float64
0.49536125725281055 <class 'float'>

标签索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
# 标签索引
s = pd.Series(np.random.rand(5), index = ['a','b','c','d','e'])
print(s)
print(s['a'],type(s['a']),s['a'].dtype)

# 如果需要选择多个标签的值,用[[]]来表示(相当于[]中包含一个列表)
# 多标签索引结果是新的数组
sci = s[['a','b','e']]
print(sci,type(sci))
>>>
a 0.714630
b 0.213957
c 0.172188
d 0.972158
e 0.875175
dtype: float64
0.714630383451 <class 'numpy.float64'> float64
a 0.714630
b 0.213957
e 0.875175
dtype: float64 <class 'pandas.core.series.Series'>
切片索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
s1 = pd.Series(np.random.rand(5))
s2 = pd.Series(np.random.rand(5), index = ['a','b','c','d','e'])
print(s1[1:4],s1[4])
print(s2['a':'c'],s2['c'])
print(s2[0:3],s2[3])
print('-----')
# 注意:用index做切片是末端包含

print(s2[:-1])
print(s2[::2])
# 下标索引做切片,和list写法一样
>>>
1 0.865967
2 0.114500
3 0.369301
dtype: float64 0.411702342342
a 0.717378
b 0.642561
c 0.391091
dtype: float64 0.39109096261
a 0.717378
b 0.642561
c 0.391091
dtype: float64 0.998978363818
-----
a 0.717378
b 0.642561
c 0.391091
d 0.998978
dtype: float64
a 0.717378
c 0.391091
e 0.957639
dtype: float64
布尔型索引
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44

# 布尔型索引
# 数组做判断之后,返回的是一个由布尔值组成的新的数组
# .isnull() / .notnull() 判断是否为空值 (None代表空值,NaN代表有问题的数值,两个都会识别为空值)
# 布尔型索引方法:用[判断条件]表示,其中判断条件可以是 一个语句,或者是 一个布尔型数组!
s = pd.Series(np.random.rand(3)*100)
s[4] = None # 添加一个空值
print(s)
bs1 = s > 50
bs2 = s.isnull()
bs3 = s.notnull()
print(bs1, type(bs1), bs1.dtype)
print(bs2, type(bs2), bs2.dtype)
print(bs3, type(bs3), bs3.dtype)
print('-----')
print(s[s > 50])
print(s[bs3])
>>>
0 2.03802
1 40.3989
2 25.2001
4 None
dtype: object
0 False
1 False
2 False
4 False
dtype: bool <class 'pandas.core.series.Series'> bool
0 False
1 False
2 False
4 True
dtype: bool <class 'pandas.core.series.Series'> bool
0 True
1 True
2 True
4 False
dtype: bool <class 'pandas.core.series.Series'> bool
-----
Series([], dtype: object)
0 2.03802
1 40.3989
2 25.2001
dtype: object
煌金 wechat
扫描关注公众号,回复「1024」获取为你准备的特别推送~
  • 本文作者: 煌金 | 微信公众号【咸鱼学Python】
  • 本文链接: http://www.xianyucoder.cn/2019/01/09/pandas1/
  • 版权声明: 本博客所有文章除特别声明外,均采用 许可协议。转载请注明出处!
  • 并保留本声明和上方二维码。感谢您的阅读和支持!