认识 DataFrame

使用 pandas 进行数据处理

Richie Cotton

Data Evangelist at DataCamp

pandas 的意义是什么?

使用 pandas 进行数据处理

课程大纲

  • 第 1 章:DataFrame
    • 排序与子集
    • 创建新列
  • 第 2 章:数据聚合
    • 汇总统计
    • 计数
    • 分组汇总统计
  • 第 3 章:切片与索引

    • 用切片取子集
    • 索引与按索引取子集
  • 第 4 章:创建与可视化数据

    • 作图
    • 处理缺失值
    • 读取数据到 DataFrame
使用 pandas 进行数据处理

pandas 构建于 NumPy 和 Matplotlib 之上

一个三角形叠在两个矩形上。三角形代表 pandas,矩形代表 NumPy 和 matplotlib。

使用 pandas 进行数据处理

pandas 很受欢迎

熊猫表情符号重复数百次,代表众多 pandas 用户。

1 https://pypistats.org/packages/pandas
使用 pandas 进行数据处理

矩形数据

名称 品种 颜色 身高(cm) 体重(kg) 出生日期
Bella 拉布拉多 棕色 56 25 2013-07-01
Charlie 贵宾犬 黑色 43 23 2016-09-16
Lucy 松狮 棕色 46 22 2014-08-25
Cooper 雪纳瑞 灰色 49 17 2011-12-11
Max 拉布拉多 黑色 59 29 2017-01-20
Stella 吉娃娃 米色 18 2 2015-04-20
Bernie 圣伯纳 白色 77 74 2018-02-27
使用 pandas 进行数据处理

pandas 的 DataFrame

print(dogs)
      name        breed  color  height_cm  weight_kg date_of_birth
0    Bella     Labrador  Brown         56         24    2013-07-01
1  Charlie       Poodle  Black         43         24    2016-09-16
2     Lucy    Chow Chow  Brown         46         24    2014-08-25
3   Cooper    Schnauzer   Gray         49         17    2011-12-11
4      Max     Labrador  Black         59         29    2017-01-20
5   Stella    Chihuahua    Tan         18          2    2015-04-20
6   Bernie  St. Bernard  White         77         74    2018-02-27
使用 pandas 进行数据处理

探索 DataFrame:.head()

print(dogs.head())
      name        breed  color  height_cm  weight_kg date_of_birth
0    Bella     Labrador  Brown         56         24    2013-07-01
1  Charlie       Poodle  Black         43         24    2016-09-16
2     Lucy    Chow Chow  Brown         46         24    2014-08-25
3   Cooper    Schnauzer   Gray         49         17    2011-12-11
4      Max     Labrador  Black         59         29    2017-01-20
使用 pandas 进行数据处理

探索 DataFrame:.info()

print(dogs.info())
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 7 entries, 0 to 6
Data columns (total 6 columns):
 #   Column         Non-Null Count  Dtype 
 --  ------         --------------  ----- 
 0   name           7 non-null      object
 1   breed          7 non-null      object
 2   color          7 non-null      object
 3   height_cm      7 non-null      int64 
 4   weight_kg      7 non-null      int64 
 5   date_of_birth  7 non-null      object
dtypes: int64(2), object(4)
memory usage: 464.0+ bytes
使用 pandas 进行数据处理

探索 DataFrame:.shape

print(dogs.shape)
(7, 6)
使用 pandas 进行数据处理

探索 DataFrame:.describe()

print(dogs.describe())
       height_cm  weight_kg
count   7.000000   7.000000
mean   49.714286  27.428571
std    17.960274  22.292429
min    18.000000   2.000000
25%    44.500000  19.500000
50%    49.000000  23.000000
75%    57.500000  27.000000
max    77.000000  74.000000
使用 pandas 进行数据处理

DataFrame 组成:.to_numpy()

print(dogs.to_numpy())
array([['Bella', 'Labrador', 'Brown', 56, 24, '2013-07-01'],
       ['Charlie', 'Poodle', 'Black', 43, 24, '2016-09-16'],
       ['Lucy', 'Chow Chow', 'Brown', 46, 24, '2014-08-25'],
       ['Cooper', 'Schnauzer', 'Gray', 49, 17, '2011-12-11'],
       ['Max', 'Labrador', 'Black', 59, 29, '2017-01-20'],
       ['Stella', 'Chihuahua', 'Tan', 18, 2, '2015-04-20'],
       ['Bernie', 'St. Bernard', 'White', 77, 74, '2018-02-27']],
      dtype=object)
使用 pandas 进行数据处理

DataFrame 组成:.columns 与 .index

print(dogs.columns)
Index(['name', 'breed', 'color', 'height_cm', 'weight_kg', 'date_of_birth'],
dtype='object')
dogs.index
RangeIndex(start=0, stop=7, step=1)
使用 pandas 进行数据处理

pandas 的哲学

应该有一种——最好只有一种——显而易见的做法。

- Tim Peters《Python 之禅》,第 13 条

一把绿色的瑞士军刀

1 https://www.python.org/dev/peps/pep-0020/
使用 pandas 进行数据处理

开始练习吧!

使用 pandas 进行数据处理

Preparing Video For Download...