pandas.DataFrame.info#
- DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, show_counts=None)[源代码]#
打印DataFrame的简洁摘要。
此方法打印 DataFrame 的信息,包括索引 dtype 和列、非 null 值以及内存使用情况。
- Parameters:
- verbosebool, optional
是否打印完整摘要。默认情况下,遵循
pandas.options.display.max_info_columns的设置。- buf可写缓冲区,默认为 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 的值消耗相同的内存量。通过深度内存检查,会以计算资源为代价执行真实的内存使用量计算。有关更多详细信息,请参阅 Frequently Asked Questions 。
- show_countsbool, optional
是否显示非 null 计数。默认情况下,仅当 DataFrame 小于
pandas.options.display.max_info_rows和pandas.options.display.max_info_columns时显示。值为 True 始终显示计数,值为 False 永远不显示计数。
- Returns:
- None
此方法打印 DataFrame 的摘要并返回 None。
参见
DataFrame.describe生成 DataFrame 列的描述性统计信息。
DataFrame.memory_usageDataFrame 列的内存使用情况。
Examples
>>> 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
打印列计数及其 dtype 的摘要,但不显示每列的信息:
>>> 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 的输出管道传输到缓冲区而不是 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