Pandas 1.0 给我们带来了什么

Pandas 1.0 给我们带来了什么

9月1.0.0日,Pandas 0.25rc发布。 该库的先前版本是 XNUMX。

第一个主要版本包含许多出色的新功能,包括改进的自动数据帧摘要、更多输出格式、新数据类型,甚至新的文档站点。

可以查看所有更改 这里,在本文中,我们将限制自己对最重要的事情进行小的、技术性较低的回顾。

您可以像平常一样安装该库 点子,但由于在撰写本文时 Pandas 1.0 仍然是 候选发布版,您需要明确指定版本:

pip install --upgrade pandas==1.0.0rc0

请注意:由于这是一个主要版本,更新可能会破坏旧代码!

顺便说一句,自该版本以来,对 Python 2 的支持已完全停止(有什么好的理由 更新 — 大约。 翻译)。 Pandas 1.0 至少需要 Python 3.6+,所以如果您不确定,请检查您安装了哪一个:

$ pip --version
pip 19.3.1 from /usr/local/lib/python3.7/site-packages/pip (python 3.7)

$ python --version
Python 3.7.5

检查 Pandas 版本的最简单方法是:

>>> import pandas as pd
>>> pd.__version__
1.0.0rc0

使用 DataFrame.info 改进自动汇总

我最喜欢的创新是方法的更新 数据框.info。 该函数变得更具可读性,使数据探索过程变得更加容易:

>>> df = pd.DataFrame({
...:   'A': [1,2,3], 
...:   'B': ["goodbye", "cruel", "world"], 
...:   'C': [False, True, False]
...:})
>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   A       3 non-null      int64
 1   B       3 non-null      object
 2   C       3 non-null      object
dtypes: int64(1), object(2)
memory usage: 200.0+ bytes

以 Markdown 格式输出表格

一个同样令人愉快的创新是能够使用以下命令将数据帧导出到 Markdown 表: DataFrame.to_markdown.

>>> df.to_markdown()
|    |   A | B       | C     |
|---:|----:|:--------|:------|
|  0 |   1 | goodbye | False |
|  1 |   2 | cruel   | True  |
|  2 |   3 | world   | False |

这使得使用 github gists 在 Medium 等网站上发布表格变得更加容易。

Pandas 1.0 给我们带来了什么

字符串和布尔值的新类型

Pandas 1.0 版本还添加了新的 试验 类型。 他们的 API 可能仍会发生变化,因此请谨慎使用。 但总的来说,Pandas 建议在任何有意义的地方使用新类型。

目前,需要明确地进行转换:

>>> B = pd.Series(["goodbye", "cruel", "world"], dtype="string")
>>> C = pd.Series([False, True, False], dtype="bool")
>>> df.B = B, df.C = C
>>> df.info()
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 3 columns):
 #   Column  Non-Null Count  Dtype
---  ------  --------------  -----
 0   A       3 non-null      int64
 1   B       3 non-null      string
 2   C       3 non-null      bool
dtypes: int64(1), object(1), string(1)
memory usage: 200.0+ bytes

注意该列如何 类型 显示新类型 - 绳子 и 布尔.

新字符串类型最有用的功能是能够选择 仅行列 来自数据帧。 这可以使解析文本数据变得更加容易:

df.select_dtypes("string")

以前,如果不明确指定名称,则无法选择行列。

您可以阅读有关新类型的更多信息 这里.

感谢您的阅读! 如前所述,可以查看完整的更改列表 这里.

来源: habr.com

添加评论