Python 中的 info() 函数

16 Apr 2025 | 3 分钟阅读

在本教程中,我们将学习 Python pandas 方法 df.info()。Pandas 是一个非常流行的库,可以轻松有效地分析数据。

它是 Python 中一个重要且广泛使用的方法。此方法打印有关 DataFrame 的信息或摘要。它打印 DataFrame 的各种信息,例如索引类型、dtype、列、非空值和内存使用情况。它提供了数据集的快速概览。

让我们看看它的语法。

语法

参数 -

  • verbose - 用于打印数据集的完整摘要。
  • buf - 这是一个可写缓冲区,默认为 sys.stdout。
  • max_cols - 指定是打印半摘要还是完整摘要。
  • memory_usage - 指定是否显示 DataFrame 元素(包括索引)的总内存使用情况。
  • null_counts - 用于显示非空计数值。

现在,让我们在 Pandas DataFrame 中理解它的用法。

info() 方法的用法

我们将使用 Iris DataFrame 来执行一些操作。它可以在 www.kaggle.com 下载。

示例 - 1

输出

      Id  SepalLengthCm  SepalWidthCm  PetalLengthCm  PetalWidthCm         Species
0      1            5.1           3.5            1.4           0.2     Iris-setosa
1      2            4.9           3.0            1.4           0.2     Iris-setosa
2      3            4.7           3.2            1.3           0.2     Iris-setosa
3      4            4.6           3.1            1.5           0.2     Iris-setosa
4      5            5.0           3.6            1.4           0.2     Iris-setosa
..   ...            ...           ...            ...           ...             ...
145  146            6.7           3.0            5.2           2.3  Iris-virginica
146  147            6.3           2.5            5.0           1.9  Iris-virginica
147  148            6.5           3.0            5.2           2.0  Iris-virginica
148  149            6.2           3.4            5.4           2.3  Iris-virginica
149  150            5.9           3.0            5.1           1.8  Iris-virginica

现在我们将打印 DataFrame 的摘要。

输出

The dataframe details is:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 6 columns):
 #   Column         Non-Null Count  Dtype
---  ------         --------------  -----
 0   Id             150 non-null    int64
 1   SepalLengthCm  150 non-null    float64
 2   SepalWidthCm   150 non-null    float64
 3   PetalLengthCm  150 non-null    float64
 4   PetalWidthCm   150 non-null    float64
 5   Species        150 non-null    object
dtypes: float64(4), int64(1), object(1)
memory usage: 7.2+ KB

解释 -

如我们所见,info() 方法打印了给定 DataFrame 的完整摘要。摘要包括所有列的列表,以及它们的数据类型和每列的非空值数量。

示例 - 2:打印 DataFrame 的简短摘要

要打印 DataFrame 的简短摘要,我们需要在 info() 方法中将 verbose 参数设置为 False。让我们来理解下面的示例。

示例 -

输出

The short summary of dataframe is:
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Columns: 6 entries, Id to Species
dtypes: float64(4), int64(1), object(1)
memory usage: 7.2+ KB

上述摘要非常简洁。当 DataFrame 中有数千个属性时,它很有帮助。

示例 - 3:排除空值计数

我们可以通过将 False 传递来省略空值计数参数。让我们来理解下面的示例。

示例 -

输出

The short summary of dataframe is:
d:/Python Project/listproblems.py:333: FutureWarning: null_counts is deprecated. Use show_counts instead
  print(df.info(null_counts=False))
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 150 entries, 0 to 149
Data columns (total 6 columns):
 #   Column         Dtype
---  ------         -----
 0   Id             int64
 1   SepalLengthCm  float64
 2   SepalWidthCm   float64
 3   PetalLengthCm  float64
 4   PetalWidthCm   float64
 5   Species        object
dtypes: float64(4), int64(1), object(1)
memory usage: 7.2+ KB

结论

我们已经讨论了 info() 方法及其索引的重要性。我们已经传递了各种参数,并打印了各种格式的摘要。