pandas.DataFrame.info#

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

打印DataFrame的简明摘要。

此方法打印有关DataFrame的信息,包括索引数据类型和列、非空值和内存使用情况。

参数
verbose布尔值,可选

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

buf可写缓冲区,默认为sys.stdout

将输出发送到何处。默认情况下,输出被打印到sys.stdout。如果需要进一步处理输出,则传递一个可写缓冲区。MAX_COLS:INT,可选,何时从详细输出切换到截断输出。如果DataFrame具有超过 max_cols 列,则使用截断的输出。默认情况下,中的设置 pandas.options.display.max_info_columns 是使用的。

memory_usageBool、str、可选

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

True始终显示内存使用情况。FALSE从不显示内存使用情况。‘Deep’的值等同于“True With深度内省”。内存使用以人类可读单位表示(以2为基数表示)。在没有深入自省的情况下,基于列数据类型和行数进行内存估计,假设对于相应的数据类型,值消耗相同的内存量。使用深度内存自检时,实际内存使用量计算是以计算资源为代价的。请参阅 Frequently Asked Questions 了解更多详细信息。

show_counts布尔值,可选

是否显示非空计数。默认情况下,仅当DataFrame小于 pandas.options.display.max_info_rowspandas.options.display.max_info_columns 。值True始终显示计数,而值False从不显示计数。

null_counts布尔值,可选

1.2.0 版后已移除: 请改用show_count。

退货

此方法打印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.core.frame.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      object
 2   float_col  5 non-null      float64
dtypes: float64(1), int64(1), object(1)
memory usage: 248.0+ bytes

打印列数及其数据类型的摘要,但不打印每列信息:

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

通过管道将DataFrame.info的输出发送到Buffer而不是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.core.frame.DataFrame'>
RangeIndex: 1000000 entries, 0 to 999999
Data columns (total 3 columns):
 #   Column    Non-Null Count    Dtype
---  ------    --------------    -----
 0   column_1  1000000 non-null  object
 1   column_2  1000000 non-null  object
 2   column_3  1000000 non-null  object
dtypes: object(3)
memory usage: 22.9+ MB
>>> df.info(memory_usage='deep')
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 1000000 entries, 0 to 999999
Data columns (total 3 columns):
 #   Column    Non-Null Count    Dtype
---  ------    --------------    -----
 0   column_1  1000000 non-null  object
 1   column_2  1000000 non-null  object
 2   column_3  1000000 non-null  object
dtypes: object(3)
memory usage: 165.9 MB