pandas.DataFrame.info#

DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, show_counts=None)[源码]#

打印 DataFrame 的简洁摘要。

此方法打印有关 DataFrame 的信息,包括索引 dtype 和列、非 NA 值以及内存使用情况。

参数:
verbosebool, optional

是否打印完整摘要。默认情况下,遵循 pandas.options.display.max_info_columns 中的设置。

bufwritable buffer, defaults to sys.stdout

输出发送到的位置。默认情况下,输出打印到 sys.stdout。如果您需要进一步处理输出,请传递一个可写缓冲区。

max_colsint, optional

何时从详细输出切换到截断输出。如果 DataFrame 的列数超过 max_cols,则使用截断输出。默认情况下,使用 pandas.options.display.max_info_columns 中的设置。

memory_usagebool, str, optional

指定是否应显示 DataFrame 元素(包括索引)的总内存使用情况。默认情况下,这遵循 pandas.options.display.memory_usage 设置。

True 始终显示内存使用情况。False 永不显示内存使用情况。“deep”值等同于“True,具有深度内省”。内存使用情况以人类可读的单位(基-2 表示法)显示。没有深度内省,内存估算基于列 dtype 和行数,假设值消耗与相应 dtype 相同的内存量。通过深度内存内省,会付出计算资源代价来执行实际的内存使用情况计算。有关更多详细信息,请参阅 常见问题解答

show_countsbool, optional

是否显示非 null 计数。默认情况下,仅当 DataFrame 小于 pandas.options.display.max_info_rowspandas.options.display.max_info_columns 时才会显示。True 值始终显示计数,False 值永不显示计数。

返回:
None

此方法打印 DataFrame 的摘要并返回 None。

另请参阅

DataFrame.describe

生成 DataFrame 列的描述性统计信息。

DataFrame.memory_usage

DataFrame 列的内存使用情况。

示例

>>> int_values = [1, 2, 3, 4, 5]
>>> text_values = ["alpha", "beta", "gamma", "delta", "epsilon"]
>>> float_values = [0.0, 0.25, 0.5, 0.75, 1.0]
>>> df = pd.DataFrame(
...     {
...         "int_col": int_values,
...         "text_col": text_values,
...         "float_col": float_values,
...     }
... )
>>> df
    int_col text_col  float_col
0        1    alpha       0.00
1        2     beta       0.25
2        3    gamma       0.50
3        4    delta       0.75
4        5  epsilon       1.00

打印所有列的信息

>>> df.info(verbose=True)
<class 'pandas.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Data columns (total 3 columns):
 #   Column     Non-Null Count  Dtype
---  ------     --------------  -----
 0   int_col    5 non-null      int64
 1   text_col   5 non-null      str
 2   float_col  5 non-null      float64
dtypes: float64(1), int64(1), str(1)
memory usage: 278.0 bytes

打印列计数及其 dtype 的摘要,但不提供每列信息

>>> df.info(verbose=False)
<class 'pandas.DataFrame'>
RangeIndex: 5 entries, 0 to 4
Columns: 3 entries, int_col to float_col
dtypes: float64(1), int64(1), str(1)
memory usage: 278.0 bytes

将 DataFrame.info 的输出管道传输到缓冲区而不是 sys.stdout,获取缓冲区内容并写入文本文件

>>> import io
>>> buffer = io.StringIO()
>>> df.info(buf=buffer)
>>> s = buffer.getvalue()
>>> with open("df_info.txt", "w", encoding="utf-8") as f:
...     f.write(s)
260

memory_usage 参数允许深度内省模式,对于大型 DataFrame 和微调内存优化特别有用

>>> random_strings_array = np.random.choice(["a", "b", "c"], 10**6)
>>> df = pd.DataFrame(
...     {
...         "column_1": np.random.choice(["a", "b", "c"], 10**6),
...         "column_2": np.random.choice(["a", "b", "c"], 10**6),
...         "column_3": np.random.choice(["a", "b", "c"], 10**6),
...     }
... )
>>> df.info()
<class 'pandas.DataFrame'>
RangeIndex: 1000000 entries, 0 to 999999
Data columns (total 3 columns):
 #   Column    Non-Null Count    Dtype
---  ------    --------------    -----
 0   column_1  1000000 non-null  str
 1   column_2  1000000 non-null  str
 2   column_3  1000000 non-null  str
dtypes: str(3)
memory usage: 25.7 MB
>>> df.info(memory_usage="deep")
<class 'pandas.DataFrame'>
RangeIndex: 1000000 entries, 0 to 999999
Data columns (total 3 columns):
 #   Column    Non-Null Count    Dtype
---  ------    --------------    -----
 0   column_1  1000000 non-null  str
 1   column_2  1000000 non-null  str
 2   column_3  1000000 non-null  str
dtypes: str(3)
memory usage: 25.7 MB