70.Pandas的应用-1.md 13.3 KB
Newer Older
J
jackfrued 已提交
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 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167 168 169 170 171 172 173 174 175 176 177 178 179 180 181 182 183 184 185 186 187 188 189 190 191 192 193 194 195 196 197 198 199 200 201 202 203 204 205 206 207 208 209 210 211 212 213 214 215 216 217 218 219 220 221 222 223 224 225 226 227 228 229 230 231 232 233 234 235 236 237 238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293 294 295 296 297 298 299 300 301 302 303 304 305 306 307 308 309 310 311 312 313 314 315 316 317 318 319 320 321 322 323 324 325 326 327 328 329 330 331 332 333 334 335 336 337 338 339 340 341 342 343 344 345 346 347 348 349 350 351 352 353 354 355 356 357 358 359 360 361 362 363 364 365 366 367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463 464 465 466 467 468 469 470 471 472 473 474 475 476 477 478 479 480 481 482 483 484 485 486 487 488 489 490 491 492 493 494 495 496 497 498 499 500 501 502 503 504 505 506 507 508 509 510 511 512 513 514 515 516 517 518 519 520 521 522 523 524 525 526 527 528 529 530 531 532 533 534 535 536 537 538 539 540 541 542 543 544 545 546 547 548 549 550 551 552 553 554 555 556 557 558 559 560 561 562 563 564 565 566 567 568 569 570 571 572 573 574 575 576 577 578 579 580 581 582 583 584 585 586 587 588 589 590 591 592 593 594 595 596 597 598 599 600 601 602 603 604 605 606 607 608 609 610 611 612 613 614 615 616 617 618 619 620 621 622 623 624 625 626 627 628 629 630 631 632 633 634 635 636 637 638 639 640 641 642 643 644 645 646 647 648 649 650 651 652 653 654 655 656 657 658 659 660 661 662 663 664 665 666 667 668 669 670 671 672 673
## Pandas的应用-1

Pandas是Wes McKinney在2008年开发的一个强大的**分析结构化数据**的工具集。Pandas以NumPy为基础(数据表示和运算),提供了用于数据处理的函数和方法,对数据分析和数据挖掘提供了很好的支持;同时Pandas还可以跟数据可视化工具Matplotlib很好的整合在一起,非常轻松愉快的实现数据的可视化展示。

Pandas核心的数据类型是`Series`(数据系列)、`DataFrame`(数据表/数据框),分别用于处理一维和二维的数据,除此之外还有一个名为`Index`的类型及其子类型,它为`Series``DataFrame`提供了索引功能。日常工作中以`DataFrame`使用最为广泛,因为二维的数据本质就是一个有行有列的表格(想一想Excel电子表格和关系型数据库中的二维表)。上述这些类型都提供了大量的处理数据的方法,数据分析师可以以此为基础实现对数据的各种常规处理。

### Series的应用

Pandas库中的`Series`对象可以用来表示一维数据结构,跟数组非常类似,但是多了一些额外的功能。`Series`的内部结构包含了两个数组,其中一个用来保存数据,另一个用来保存数据的索引。

#### 创建Series对象

> **提示**:在执行下面的代码之前,请先导入`pandas`以及相关的库文件,具体的做法可以参考上一章。

##### 方法1:通过列表或数组创建Series对象

代码:

```Python
# data参数表示数据,index参数表示数据的索引(标签)
# 如果没有指定index属性,默认使用数字索引
ser1 = pd.Series(data=[320, 180, 300, 405], index=['一季度', '二季度', '三季度', '四季度'])
ser1
```

输出:

```
一季度    320
二季度    180
三季度    300
四季度    405
dtype: int64
```

##### 方法2:通过字典创建Series对象。

代码:

```Python
# 字典中的键就是数据的索引(标签),字典中的值就是数据
ser2 = pd.Series({'一季度': 320, '二季度': 180, '三季度': 300, '四季度': 405})
ser2
```

输出:

```
一季度    320
二季度    180
三季度    300
四季度    405
dtype: int64
```

#### 索引和切片

跟数组一样,Series对象也可以进行索引和切片操作,不同的是Series对象因为内部维护了一个保存索引的数组,所以除了可以使用整数索引通过位置检索数据外,还可以通过自己设置的索引标签获取对应的数据。

##### 使用整数索引

代码:

```Python
print(ser2[0], ser[1], ser[2], ser[3])
ser2[0], ser2[3] = 350, 360
print(ser2)
```

输出:

```
320 180 300 405
一季度    350
二季度    180
三季度    300
四季度    360
dtype: int64
```

> **提示**:如果要使用负向索引,必须在创建`Series`对象时通过`index`属性指定非数值类型的标签。

##### 使用自定义的标签索引

代码:

```Python
print(ser2['一季度'], ser2['三季度'])
ser2['一季度'] = 380
print(ser2)
```

输出:

```
350 300
一季度    380
二季度    180
三季度    300
四季度    360
dtype: int64
```

##### 切片操作

代码:

```Python
print(ser2[1:3])
print(ser2['二季度':'四季度'])
```

输出:

```
二季度    180
三季度    300
dtype: int64
二季度    500
三季度    500
四季度    520
dtype: int64
```

代码:

```Python
ser2[1:3] = 400, 500
ser2
```

输出:

```
一季度    380
二季度    400
三季度    500
四季度    360
dtype: int64
```

##### 花式索引

代码:

```Python
print(ser2[['二季度', '四季度']])
ser2[['二季度', '四季度']] = 500, 520
print(ser2)
```

输出:

```
二季度    400
四季度    360
dtype: int64
一季度    380
二季度    500
三季度    500
四季度    520
dtype: int64
```

##### 布尔索引

代码:

```Python
ser2[ser2 >= 500]
```

输出:

```
二季度    500
三季度    500
四季度    520
dtype: int64
```

####属性和方法

Series对象的常用属性如下表所示。

| 属性                      | 说明                                    |
| ------------------------- | --------------------------------------- |
| `dtype` / `dtypes`        | 返回`Series`对象的数据类型              |
| `hasnans`                 | 判断`Series`对象中有没有空值            |
| `at` / `iat`              | 通过索引访问`Series`对象中的单个值      |
| `loc` / `iloc`            | 通过一组索引访问`Series`对象中的一组值  |
| `index`                   | 返回`Series`对象的索引                 |
| `is_monotonic`            | 判断`Series`对象中的数据是否单调        |
| `is_monotonic_increasing` | 判断`Series`对象中的数据是否单调递增    |
| `is_monotonic_decreasing` | 判断`Series`对象中的数据是否单调递减    |
| `is_unique`               | 判断`Series`对象中的数据是否独一无二    |
| `size`                    | 返回`Series`对象中元素的个数            |
| `values`                  | 以`ndarray`的方式返回`Series`对象中的值 |

`Series`对象的方法很多,我们通过下面的代码为大家介绍一些常用的方法。

##### 统计相关的方法

`Series`对象支持各种获取描述性统计信息的方法。

代码:

```Python
# 求和
print(ser2.sum())
# 求均值
print(ser2.mean())
# 求最大
print(ser2.max())
# 求最小
print(ser2.min())
# 计数
print(ser2.count())
# 求标准差
print(ser2.std())
# 求方差
print(ser2.var())
# 求中位数
print(ser2.median())
```

`Series`对象还有一个名为`describe()`的方法,可以获得上述所有的描述性统计信息,如下所示。

代码:

```Python
ser2.describe()
```

输出:

```
count      4.000000
mean     475.000000
std       64.031242
min      380.000000
25%      470.000000
50%      500.000000
75%      505.000000
max      520.000000
dtype: float64
```

> **提示**:因为`describe()`返回的也是一个`Series`对象,所以也可以用`ser2.describe()['mean']`来获取平均值。

如果`Series`对象有重复的值,我们可以使用`unique()`方法获得去重之后的`Series`对象;可以使用`nunique()`方法统计不重复值的数量;如果想要统计每个值重复的次数,可以使用`value_counts()`方法,这个方法会返回一个`Series`对象,它的索引就是原来的`Series`对象中的值,而每个值出现的次数就是返回的`Series`对象中的数据,在默认情况下会按照出现次数做降序排列。

代码:

```Python
ser3 = pd.Series(data=['apple', 'banana', 'apple', 'pitaya', 'apple', 'pitaya', 'durian'])
ser3.value_counts()
```

输出:

```
apple     3
pitaya    2
durian    1
banana    1
dtype: int64
```

代码:

```Python
ser3.nunique()
```

输出:

```
4
```

##### 数据处理的方法

`Series`对象的`isnull()``notnull()`方法可以用于空值的判断,代码如下所示。

代码:

```Python
ser4 = pd.Series(data=[10, 20, np.NaN, 30, np.NaN])
ser4.isnull()
```

输出:

```
0    False
1    False
2     True
3    False
4     True
dtype: bool
```

代码:

```Python
ser4.notnull()
```

输出:

```
0     True
1     True
2    False
3     True
4    False
dtype: bool
```

`Series`对象的`dropna()``fillna()`方法分别用来删除空值和填充空值,具体的用法如下所示。

代码:

```Python
ser4.dropna()
```

输出:

```
0    10.0
1    20.0
3    30.0
dtype: float64
```

代码:

```Python
# 将空值填充为40
ser4.fillna(value=40)
```

输出:

```
0    10.0
1    20.0
2    40.0
3    30.0
4    40.0
dtype: float64
```

代码:

```Python
# backfill或bfill表示用后一个元素的值填充空值
# ffill或pad表示用前一个元素的值填充空值
ser4.fillna(method='ffill')
```

输出:

```
0    10.0
1    20.0
2    20.0
3    30.0
4    30.0
dtype: float64
```

需要提醒大家注意的是,`dropna()``fillna()`方法都有一个名为`inplace`的参数,它的默认值是`False`,表示删除空值或填充空值不会修改原来的`Series`对象,而是返回一个新的`Series`对象来表示删除或填充空值后的数据系列,如果将`inplace`参数的值修改为`True`,那么删除或填充空值会就地操作,直接修改原来的`Series`对象,那么方法的返回值是`None`。后面我们会接触到的很多方法,包括`DataFrame`对象的很多方法都会有这个参数,它们的意义跟这里是一样的。

`Series`对象的`mask()``where()`方法可以将满足或不满足条件的值进行替换,如下所示。

代码:

```Python
ser5 = pd.Series(range(5))
ser5.where(ser5 > 0)
```

输出:

```
0    NaN
1    1.0
2    2.0
3    3.0
4    4.0
dtype: float64
```

代码:

```Python
ser5.where(ser5 > 1, 10)
```

输出:

```
0    10
1    10
2     2
3     3
4     4
dtype: int64
```

代码:

```Python
ser5.mask(ser5 > 1, 10)
```

输出:

```
0     0
1     1
2    10
3    10
4    10
dtype: int64
```

`Series`对象的`duplicated()`方法可以帮助我们找出重复的数据,而`drop_duplicates()`方法可以帮我们删除重复数据。

代码:

```Python
ser3.duplicated()
```

输出:

```
0    False
1    False
2     True
3    False
4     True
5     True
6    False
dtype: bool
```

代码:

```Python
ser3.drop_duplicates()
```

输出:

```
0     apple
1    banana
3    pitaya
6    durian
dtype: object
```

`Series`对象的`apply()``map()`方法非常重要,它们可以用于数据处理,把数据映射或转换成我们期望的样子,这个操作在数据分析的数据准备阶段非常重要。

代码:

```Python
ser6 = pd.Series(['cat', 'dog', np.nan, 'rabbit'])
ser6
```

输出:

```
0       cat
1       dog
2       NaN
3    rabbit
dtype: object
```

代码:

```Python
ser6.map({'cat': 'kitten', 'dog': 'puppy'})
```

输出:

```
0    kitten
1     puppy
2       NaN
3       NaN
dtype: object
```

代码:

```Python
ser6.map('I am a {}'.format, na_action='ignore')
```

输出:

```
0       I am a cat
1       I am a dog
2              NaN
3    I am a rabbit
dtype: object
```

代码:

```Python
ser7 = pd.Series([20, 21, 12],  index=['London', 'New York', 'Helsinki'])
ser7
```

输出:

```
London      20
New York    21
Helsinki    12
dtype: int64
```

代码:

```Python
ser7.apply(np.square)
```

输出:

```
London      400
New York    441
Helsinki    144
dtype: int64
```

代码:

```Python
ser7.apply(lambda x, value: x - value, args=(5, ))
```

输出:

```
London      15
New York    16
Helsinki     7
dtype: int64
```

##### 排序和取头部值的方法

`Series`对象的`sort_index()``sort_values()`方法可以用于对索引和数据的排序,排序方法有一个名为`ascending`的布尔类型参数,该参数用于控制排序的结果是升序还是降序;而名为`kind`的参数则用来控制排序使用的算法,默认使用了`quicksort`,也可以选择`mergesort``heapsort`;如果存在空值,那么可以用`na_position`参数空值放在最前还是最后,默认是`last`,代码如下所示。

代码:

```Python
ser8 = pd.Series(
    data=[35, 96, 12, 57, 25, 89], 
index=['grape', 'banana', 'pitaya', 'apple', 'peach', 'orange']
)
# 按值从小到大排序
ser8.sort_values()
```

输出:

```
pitaya    12
peach     25
grape     35
apple     57
orange    89
banana    96
dtype: int64
```

代码:

```Python
# 按索引从大到小排序
ser8.sort_index(ascending=False)
```

输出:

```
pitaya    12
peach     25
orange    89
grape     35
banana    96
apple     57
dtype: int64
```

如果要从`Series`对象中找出元素中最大或最小的“Top-N”,实际上是不需要对所有的值进行排序的,可以使用`nlargest()``nsmallest()`方法来完成,如下所示。

代码:

```Python
# 值最大的3个
ser8.nlargest(3)
```

输出:

```
banana    96
orange    89
apple     57
dtype: int64
```

代码:

```Python
# 值最小的2个
ser8.nsmallest(2)
```

输出:

```
pitaya    12
peach     25
dtype: int64
```

#### 绘制图表

Series对象有一个名为`plot`的方法可以用来生成图表,如果选择生成折线图、饼图、柱状图等,默认会使用Series对象的索引作为横坐标,使用Series对象的数据作为纵坐标。

首先导入`matplotlib``pyplot`模块并进行必要的配置。

```Python
import matplotlib.pyplot as plt

# 配置支持中文的非衬线字体(默认的字体无法显示中文)
plt.rcParams['font.sans-serif'] = ['SimHei', ]
# 使用指定的中文字体时需要下面的配置来避免负号无法显示
plt.rcParams['axes.unicode_minus'] = False
```

创建`Series`对象并绘制对应的柱状图。

```Python
ser9 = pd.Series({'一季度': 400, '二季度': 520, '三季度': 180, '四季度': 380})
# 通过Series对象的plot方法绘图(kind='bar'表示绘制柱状图)
ser9.plot(kind='bar', color=['r', 'g', 'b', 'y'])
# x轴的坐标旋转到0度(中文水平显示)
plt.xticks(rotation=0)
# 在柱状图的柱子上绘制数字
for i in range(4):
    plt.text(i, ser9[i] + 5, ser9[i], ha='center')
# 显示图像
plt.show()
```

骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
674
![](https://github.com/jackfrued/mypic/raw/master/20220619171513.png)
J
jackfrued 已提交
675 676 677 678 679 680 681 682 683 684 685

绘制反映每个季度占比的饼图。

```Python
# autopct参数可以配置在饼图上显示每块饼的占比
ser9.plot(kind='pie', autopct='%.1f%%')
# 设置y轴的标签(显示在饼图左侧的文字)
plt.ylabel('各季度占比')
plt.show()
```

骆昊的技术专栏's avatar
骆昊的技术专栏 已提交
686
![](https://github.com/jackfrued/mypic/raw/master/20220619171503.png)