Pandas 2.2 中文官方教程和指南(一)(3)

简介: Pandas 2.2 中文官方教程和指南(一)

Pandas 2.2 中文官方教程和指南(一)(2)https://developer.aliyun.com/article/1510593

pandas 数据表表示


  • 我想存储泰坦尼克号的乘客数据。 对于许多乘客,我知道姓名(字符),年龄(整数)和性别(男/女)数据。
In [2]: df = pd.DataFrame(
 ...:    {
 ...:        "Name": [
 ...:            "Braund, Mr. Owen Harris",
 ...:            "Allen, Mr. William Henry",
 ...:            "Bonnell, Miss. Elizabeth",
 ...:        ],
 ...:        "Age": [22, 35, 58],
 ...:        "Sex": ["male", "male", "female"],
 ...:    }
 ...: )
 ...: 
In [3]: df
Out[3]: 
 Name  Age     Sex
0   Braund, Mr. Owen Harris   22    male
1  Allen, Mr. William Henry   35    male
2  Bonnell, Miss. Elizabeth   58  female 
  • 要手动将数据存储在表中,请创建一个DataFrame。 使用 Python 字典列表时,字典键将用作列标题,每个列表中的值将用作DataFrame的列。

一个DataFrame是一个可以在列中存储不同类型数据(包括字符、整数、浮点值、分类数据等)的二维数据结构。 它类似于电子表格、SQL 表或 R 中的data.frame

  • 表格有 3 列,每列都有一个列标签。 列标签分别是NameAgeSex
  • Name由文本数据组成,每个值都是一个字符串,列Age是数字,列Sex是文本数据。

在电子表格软件中,我们的数据的表格表示看起来会非常相似:

DataFrame中的每一列都是一个Series


  • 我只对在Age列中的数据感兴趣
In [4]: df["Age"]
Out[4]: 
0    22
1    35
2    58
Name: Age, dtype: int64 
  • 当选择 pandas DataFrame的单个列时,结果是一个 pandas Series。 要选择列,请在方括号[]之间使用列标签。

注意

如果您熟悉 Python dictionaries,选择单个列与基于键选择字典值非常相似。

你也可以从头开始创建一个Series

In [5]: ages = pd.Series([22, 35, 58], name="Age")
In [6]: ages
Out[6]: 
0    22
1    35
2    58
Name: Age, dtype: int64 

pandas 的Series没有列标签,因为它只是DataFrame的单列。 Series 确实有行标签。

对 DataFrame 或 Series 执行某些操作

  • 我想知道乘客的最大年龄
    我们可以通过选择Age列并应用max()DataFrame上执行此操作:
In [7]: df["Age"].max()
Out[7]: 58 
  • 或者到Series
In [8]: ages.max()
Out[8]: 58 

正如 max() 方法所示,您可以使用 DataFrameSeries 执行 操作。pandas 提供了许多功能,每个功能都是您可以应用于 DataFrameSeries方法。由于方法是函数,不要忘记使用括号 ()

  • 我对我的数据表的数值数据进行一些基本统计感兴趣
In [9]: df.describe()
Out[9]: 
 Age
count   3.000000
mean   38.333333
std    18.230012
min    22.000000
25%    28.500000
50%    35.000000
75%    46.500000
max    58.000000 
  • describe() 方法提供了对 DataFrame 中数值数据的快速概述。由于 NameSex 列是文本数据,默认情况下不会被 describe() 方法考虑在内。

许多 pandas 操作会返回一个 DataFrame 或一个 Seriesdescribe() 方法就是一个返回 pandas Series 或 pandas DataFrame 的 pandas 操作的示例。

转至用户指南

在用户指南的关于 使用 describe 进行汇总的部分中查看更多选项

注意

这只是一个起点。与电子表格软件类似,pandas 将数据表示为具有列和行的表格。除了表示外,还有您在电子表格软件中进行的数据操作和计算,pandas 也支持。继续阅读下一篇教程,开始使用!

记住
  • 导入包,即 import pandas as pd
  • 数据表以 pandas 的 DataFrame 形式存储
  • DataFrame 中的每一列都是一个 Series
  • 您可以通过将方法应用于 DataFrameSeries 来执行操作

转至用户指南

关于 DataFrameSeries 的更详细解释在数据结构简介中提供。

pandas 数据表表示


  • 我想存储 Titanic 的乘客数据。对于许多乘客,我知道他们的姓名(字符)、年龄(整数)和性别(男性/女性)数据。
In [2]: df = pd.DataFrame(
 ...:    {
 ...:        "Name": [
 ...:            "Braund, Mr. Owen Harris",
 ...:            "Allen, Mr. William Henry",
 ...:            "Bonnell, Miss. Elizabeth",
 ...:        ],
 ...:        "Age": [22, 35, 58],
 ...:        "Sex": ["male", "male", "female"],
 ...:    }
 ...: )
 ...: 
In [3]: df
Out[3]: 
 Name  Age     Sex
0   Braund, Mr. Owen Harris   22    male
1  Allen, Mr. William Henry   35    male
2  Bonnell, Miss. Elizabeth   58  female 
  • 要手动存储数据到表格中,创建一个 DataFrame。当使用 Python 字典的列表时,字典的键将被用作列标题,每个列表中的值将作为 DataFrame 的列。

DataFrame 是一种二维数据结构,可以在列中存储不同类型的数据(包括字符、整数、浮点值、分类数据等)。它类似于电子表格、SQL 表或 R 中的 data.frame

  • 表格有 3 列,每列都有一个列标签。列标签分别是 NameAgeSex
  • Name 包含文本数据,每个值为字符串,列 Age 是数字,列 Sex 是文本数据。

在电子表格软件中,我们的数据的表格表示看起来会非常相似:

每个DataFrame中的列都是一个Series


  • 我只对Age列中的数据感兴趣
In [4]: df["Age"]
Out[4]: 
0    22
1    35
2    58
Name: Age, dtype: int64 
  • 当选择 pandas DataFrame的单个列时,结果是一个 pandas Series。要选择列,请在方括号[]之间使用列标签。

注意

如果你熟悉 Python dictionaries,选择单个列与基于键选择字典值非常相似。

你也可以从头开始创建一个Series

In [5]: ages = pd.Series([22, 35, 58], name="Age")
In [6]: ages
Out[6]: 
0    22
1    35
2    58
Name: Age, dtype: int64 

一个 pandas Series没有列标签,因为它只是一个DataFrame的单列。一个 Series 有行标签。

DataFrameSeries执行一些操作

  • 我想知道乘客的最大年龄
    我们可以通过选择Age列并应用max()来对DataFrame进行操作:
In [7]: df["Age"].max()
Out[7]: 58 
  • 或对Series进行操作:
In [8]: ages.max()
Out[8]: 58 

正如max()方法所示,你可以对DataFrameSeries执行操作。pandas 提供了许多功能,每个功能都是可以应用于DataFrameSeries方法。由于方法是函数,请不要忘记使用括号()

  • 我对我的数据表的数值数据感兴趣的一些基本统计信息
In [9]: df.describe()
Out[9]: 
 Age
count   3.000000
mean   38.333333
std    18.230012
min    22.000000
25%    28.500000
50%    35.000000
75%    46.500000
max    58.000000 
  • describe()方法提供了DataFrame中数值数据的快速概述。由于NameSex列是文本数据,默认情况下不会被describe()方法考虑在内。

许多 pandas 操作会返回一个DataFrame或一个Seriesdescribe()方法就是一个返回 pandas Series或 pandas DataFrame的 pandas 操作的示例。

转到用户指南

在用户��南的关于使用 describe 进行聚合部分查看更多关于describe的选项

注意

这只是一个起点。与电子表格软件类似,pandas 将数据表示为具有列和行的表格。除了表示,pandas 还支持电子表格软件中的数据操作和计算。继续阅读下一个教程以开始!

记住
  • 导入包,即import pandas as pd
  • 数据表以 pandas DataFrame的形式存储
  • 每个DataFrame中的列都是一个Series
  • 你可以通过将方法应用于 DataFrameSeries 来完成任务。

前往用户指南

关于 DataFrameSeries 的更详细解释可在数据结构介绍中找到。

如何读取和写入表格数据?

原文:pandas.pydata.org/docs/getting_started/intro_tutorials/02_read_write.html


  • 我想分析泰坦尼克号乘客数据,该数据以 CSV 文件的形式提供。
In [2]: titanic = pd.read_csv("data/titanic.csv") 
  • pandas 提供read_csv()函数,将存储为 csv 文件的数据读取到 pandas 的DataFrame中。pandas 支持许多不同的文件格式或数据源(csv、excel、sql、json、parquet 等),每个都带有前缀read_*

在读取数据后,务必始终检查数据。显示DataFrame时,默认会显示前后 5 行:

In [3]: titanic
Out[3]: 
 PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked
0              1         0       3  ...   7.2500   NaN         S
1              2         1       1  ...  71.2833   C85         C
2              3         1       3  ...   7.9250   NaN         S
3              4         1       1  ...  53.1000  C123         S
4              5         0       3  ...   8.0500   NaN         S
..           ...       ...     ...  ...      ...   ...       ...
886          887         0       2  ...  13.0000   NaN         S
887          888         1       1  ...  30.0000   B42         S
888          889         0       3  ...  23.4500   NaN         S
889          890         1       1  ...  30.0000  C148         C
890          891         0       3  ...   7.7500   NaN         Q
[891 rows x 12 columns] 
  • 我想看一下 pandas DataFrame 的前 8 行。
In [4]: titanic.head(8)
Out[4]: 
 PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked
0            1         0       3  ...   7.2500   NaN         S
1            2         1       1  ...  71.2833   C85         C
2            3         1       3  ...   7.9250   NaN         S
3            4         1       1  ...  53.1000  C123         S
4            5         0       3  ...   8.0500   NaN         S
5            6         0       3  ...   8.4583   NaN         Q
6            7         0       1  ...  51.8625   E46         S
7            8         0       3  ...  21.0750   NaN         S
[8 rows x 12 columns] 
  • 要查看DataFrame的前 N 行,请使用head()方法,并将所需的行数(在本例中为 8)作为参数。

注意

对最后 N 行感兴趣吗?pandas 还提供了tail()方法。例如,titanic.tail(10)将返回 DataFrame 的最后 10 行。

通过请求 pandas 的dtypes属性,可以检查 pandas 如何解释每列的数据类型:

In [5]: titanic.dtypes
Out[5]: 
PassengerId      int64
Survived         int64
Pclass           int64
Name            object
Sex             object
Age            float64
SibSp            int64
Parch            int64
Ticket          object
Fare           float64
Cabin           object
Embarked        object
dtype: object 

对于每列,列出了使用的数据类型。此DataFrame中的数据类型为整数(int64)、浮点数(float64)和字符串(object)。

注意

请求dtypes时,不使用括号!dtypesDataFrameSeries的属性。DataFrameSeries的属性不需要括号。属性表示DataFrame/Series的特征,而方法(需要括号)在第一个教程中介绍了DataFrame/Series的操作。

  • 我的同事请求将泰坦尼克号数据作为电子表格。
In [6]: titanic.to_excel("titanic.xlsx", sheet_name="passengers", index=False) 
  • read_*函数用于将数据读取到 pandas 中,to_*方法用于存储数据。to_excel()方法将数据存储为 excel 文件。在此示例中,sheet_name命名为passengers,而不是默认的Sheet1。通过设置index=False,行索引标签不会保存在电子表格中。

等效的读取函数read_excel()将重新加载数据到DataFrame中:

In [7]: titanic = pd.read_excel("titanic.xlsx", sheet_name="passengers") 
In [8]: titanic.head()
Out[8]: 
 PassengerId  Survived  Pclass  ...     Fare Cabin  Embarked
0            1         0       3  ...   7.2500   NaN         S
1            2         1       1  ...  71.2833   C85         C
2            3         1       3  ...   7.9250   NaN         S
3            4         1       1  ...  53.1000  C123         S
4            5         0       3  ...   8.0500   NaN         S
[5 rows x 12 columns] 
  • 我对DataFrame的技术摘要感兴趣
In [9]: titanic.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 891 entries, 0 to 890
Data columns (total 12 columns):
 #   Column       Non-Null Count  Dtype 
---  ------       --------------  ----- 
 0   PassengerId  891 non-null    int64 
 1   Survived     891 non-null    int64 
 2   Pclass       891 non-null    int64 
 3   Name         891 non-null    object 
 4   Sex          891 non-null    object 
 5   Age          714 non-null    float64
 6   SibSp        891 non-null    int64 
 7   Parch        891 non-null    int64 
 8   Ticket       891 non-null    object 
 9   Fare         891 non-null    float64
 10  Cabin        204 non-null    object 
 11  Embarked     889 non-null    object 
dtypes: float64(2), int64(5), object(5)
memory usage: 83.7+ KB 
  • info()方法提供有关DataFrame的技术信息,让我们更详细地解释输出:
  • 确实是一个DataFrame
  • 有 891 个条目,即 891 行。
  • 每行都有一个行标签(又称index),其值范围从 0 到 890。
  • 表格有 12 列。大多数列在每一行都有一个值(所有 891 个值都是non-null)。一些列确实有缺失值,少于 891 个non-null值。
  • NameSexCabinEmbarked由文本数据(字符串,又称object)组成。其他列是数值数据,其中一些是整数(又称integer),另一些是实数(又称float)。
  • 不同列中的数据类型(字符、整数等)通过列出dtypes进行总结。
  • 提供了用于保存 DataFrame 的大致 RAM 使用量。
记住
  • 通过read_*函数支持从许多不同文件格式或数据源将数据导入 pandas。
  • 通过不同的to_*方法提供了将数据导出到 pandas 的功能。
  • head/tail/info方法和dtypes属性对于初步检查很方便。

到用户指南

有关从 pandas 到输入和输出的完整概述,请参阅有关读取器和写入器函数的用户指南部分。

如何选择 DataFrame 的子集?

原文:pandas.pydata.org/docs/getting_started/intro_tutorials/03_subset_data.html

如何从DataFrame中选择特定列?


  • 我对泰坦尼克号乘客的年龄感兴趣。
In [4]: ages = titanic["Age"]
In [5]: ages.head()
Out[5]: 
0    22.0
1    38.0
2    26.0
3    35.0
4    35.0
Name: Age, dtype: float64 
  • 要选择单个列,请使用方括号[]与感兴趣的列名。

每个DataFrame中的列都是一个Series。当选择单个列时,返回的对象是一个 pandas Series。我们可以通过检查输出的类型来验证这一点:

In [6]: type(titanic["Age"])
Out[6]: pandas.core.series.Series 

并查看输出的shape

In [7]: titanic["Age"].shape
Out[7]: (891,) 

DataFrame.shape 是一个属性(记住读写教程中不要对属性使用括号), 用于包含行数和列数的 pandas SeriesDataFrame(nrows, ncolumns)。pandas Series 是一维的,只返回行数。

  • 我对泰坦尼克号乘客的年龄和性别感兴趣。
In [8]: age_sex = titanic[["Age", "Sex"]]
In [9]: age_sex.head()
Out[9]: 
 Age     Sex
0  22.0    male
1  38.0  female
2  26.0  female
3  35.0  female
4  35.0    male 
  • 要选择多个列,请在选择括号[]内使用列名列表。

注意

内部方括号定义了一个Python 列表,其中包含列名,而外部方括号用于从 pandas DataFrame 中选择数据,就像在前面的示例中看到的那样。

返回的数据类型是一个 pandas DataFrame:

In [10]: type(titanic[["Age", "Sex"]])
Out[10]: pandas.core.frame.DataFrame 
In [11]: titanic[["Age", "Sex"]].shape
Out[11]: (891, 2) 

选择返回了一个具有 891 行和 2 列的DataFrame。记住,DataFrame 是二维的,具有行和列两个维度。

转到用户指南

有关索引的基本信息,请参阅用户指南中关于索引和选择数据的部分。

Pandas 2.2 中文官方教程和指南(一)(4)https://developer.aliyun.com/article/1510595

相关文章
|
1月前
|
存储 JSON 数据格式
Pandas 使用教程 CSV - CSV 转 JSON
Pandas 使用教程 CSV - CSV 转 JSON
22 0
|
1月前
|
JSON 数据格式 Python
Pandas 使用教程 JSON
Pandas 使用教程 JSON
26 0
|
1月前
|
SQL 数据采集 JSON
Pandas 使用教程 Series、DataFrame
Pandas 使用教程 Series、DataFrame
34 0
|
3月前
|
数据采集 存储 数据可视化
Pandas高级教程:数据清洗、转换与分析
Pandas是Python的数据分析库,提供Series和DataFrame数据结构及数据分析工具,便于数据清洗、转换和分析。本教程涵盖Pandas在数据清洗(如缺失值、重复值和异常值处理)、转换(数据类型转换和重塑)和分析(如描述性统计、分组聚合和可视化)的应用。通过学习Pandas,用户能更高效地处理和理解数据,为数据分析任务打下基础。
299 3
|
4月前
|
索引 Python
Pandas 2.2 中文官方教程和指南(一)(4)
Pandas 2.2 中文官方教程和指南(一)
47 0
|
4月前
|
XML 关系型数据库 PostgreSQL
Pandas 2.2 中文官方教程和指南(一)(2)
Pandas 2.2 中文官方教程和指南(一)
121 0
|
4月前
|
XML 关系型数据库 MySQL
Pandas 2.2 中文官方教程和指南(一)(1)
Pandas 2.2 中文官方教程和指南(一)
128 0
|
4月前
|
C++ 索引 Python
Pandas 2.2 中文官方教程和指南(五)(4)
Pandas 2.2 中文官方教程和指南(五)
38 0
|
4月前
|
索引 Python
Pandas 2.2 中文官方教程和指南(五)(3)
Pandas 2.2 中文官方教程和指南(五)
37 0
|
4月前
|
SQL API 数据格式
Pandas 2.2 中文官方教程和指南(五)(2)
Pandas 2.2 中文官方教程和指南(五)
41 0