# Pandas

> 原文：[https://www.bookbookmark.ds100.org/ch/20/ref_pandas.html](https://www.bookbookmark.ds100.org/ch/20/ref_pandas.html)

```
# HIDDEN
# Clear previously defined variables
%reset -f

# Set directory for data loading to work properly
import os
os.chdir(os.path.expanduser('~/notebooks/20'))

```

| 功能 | 第章 | 说明 |
| --- | --- | --- |
| [`pd.DataFrame(data)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.html) | 表格数据和 Pandas | 从二维数组或字典创建数据帧`data` |
| [`pd.read_csv(filepath)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_csv.html) | Tabular Data and pandas | 将 csv 文件从`filepath`导入为 Pandas 数据帧 |
| 【HTG2】【HTG0】【HTG3】【HTG4】【HTG5】【HTG1】【HTG6】 | Tabular Data and pandas | 查看数据帧或序列的第一行`n` |
| 【HTG0】【HTG2】【HTG1】 | Tabular Data and pandas | 查看数据帧的索引和列值 |
| 【HTG2】【HTG0】【HTG3】【HTG4】【HTG5】【HTG1】【HTG6】 | 探索性数据分析 | 查看有关数据帧或序列的描述性统计信息 |
| [`pd.Series.unique()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.unique.html) | Exploratory Data Analysis | 查看序列中的唯一值 |
| [`pd.Series.value_counts()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.value_counts.html) | Exploratory Data Analysis | 查看序列中每个唯一值出现的次数 |
| `df[col]` | Tabular Data and pandas | 从数据帧`df`返回列`col`作为一个系列 |
| `df[[col]]` | Tabular Data and pandas | 从数据帧`df`返回列`col`作为数据帧 |
| `df.loc[row, col]` | Tabular Data and pandas | 从 dataframe`df`返回索引名为`row`和列名为`col`的行；`row`也可以是布尔序列 |
| `df.iloc[row, col]` | Tabular Data and pandas | 从数据帧`df`返回索引号为`row`和列号为`col`的行；`row`也可以是布尔序列 |
| 【HTG2】【HTG0】【HTG3】【HTG4】【HTG5】【HTG1】【HTG6】 | 数据清理 | 查看数据帧或序列中缺少的值 |
| 【HTG2】【HTG0】【HTG3】【HTG4】【HTG5】【HTG1】【HTG6】 | Data Cleaning | 用`value`填充数据帧或序列中缺少的值 |
| 【HTG2】【HTG0】【HTG3】【HTG4】【HTG5】【HTG1】【HTG6】 | Data Cleaning | 从数据帧或序列中删除缺少值的行或列 |
| [`pd.DataFrame.drop(labels, axis)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.drop.html) | Data Cleaning | 沿`axis`从数据框中删除名为`labels`的行或列 |
| [`pd.DataFrame.rename()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.rename.html) | Data Cleaning | 重命名数据帧中的指定行或列 |
| [`pd.DataFrame.replace(to_replace, value)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.replace.html) | Data Cleaning | 在数据帧中用`value`替换`to_replace`值 |
| [`pd.DataFrame.reset_index(drop=False)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.reset_index.html) | Data Cleaning | 重置数据帧的索引；默认情况下，将旧索引保留为新列，除非指定了`drop=True` |
| [`pd.DataFrame.sort_values(by, ascending=True)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.sort_values.html) | Tabular Data and pandas | 按指定列`by`对数据帧排序，默认为升序 |
| [`pd.DataFrame.groupby(by)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.groupby.html) | Tabular Data and pandas | 返回 GroupBy 对象，该对象包含按指定列中的值分组的数据帧`by` |
| [`GroupBy.&lt;function&gt;`](https://pandas.pydata.org/pandas-docs/stable/api.html#id41) | Tabular Data and pandas | 将函数`&lt;function&gt;`应用于 groupby 对象`GroupBy`中的每个组，例如[`mean()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.mean.html)，[`count()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.core.groupby.GroupBy.count.html) |
| [`pd.Series.&lt;function&gt;`](https://pandas.pydata.org/pandas-docs/stable/api.html#computations-descriptive-stats) | Tabular Data and pandas | 将函数`&lt;function&gt;`应用于具有数值的序列；例如[`mean()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.mean.html)、[`max()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.max.html)、[`median()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.median.html) |
| [`pd.Series.str.&lt;function&gt;`](https://pandas.pydata.org/pandas-docs/stable/api.html#string-handling) | Tabular Data and pandas | 将函数`&lt;function&gt;`应用于具有字符串值的序列；例如[`len()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.len.html)、[`lower()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.lower.html)、[`split()`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.str.split.html) |
| [`pd.Series.dt.&lt;property&gt;`](https://pandas.pydata.org/pandas-docs/stable/api.html#datetimelike-properties) | Tabular Data and pandas | 从具有日期时间值的序列中提取属性`&lt;property&gt;`，例如[`year`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.year.html)，[`month`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.month.html)，[`date`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.Series.dt.date.html) |
| [`pd.get_dummies(columns, drop_first=False)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.get_dummies.html) | --- | 将分类变量`columns`转换为虚拟变量；默认值保留所有变量，除非指定了`drop_first=True`。 |
| [`pd.merge(left, right, how, on)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.DataFrame.merge.html) | 探索性数据分析；数据库和 SQL | 在指定的列`on`上将两个数据帧`left`和`right`合并在一起；连接类型取决于`how` |
| [`pd.read_sql(sql, con)`](https://pandas.pydata.org/pandas-docs/stable/generated/pandas.read_sql.html) | 数据库和 SQL | 读取数据库连接`con`上的 SQL 查询`sql`，并将结果作为 Pandas 数据帧返回 |