获取 DataFrame 信息和内存使用情况

要获取有关 DataFrame 的基本信息,包括列名和数据类型:

import pandas as pd

df = pd.DataFrame({'integers': [1, 2, 3], 
                   'floats': [1.5, 2.5, 3], 
                   'text': ['a', 'b', 'c'], 
                   'ints with None': [1, None, 3]})

df.info()
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 4 columns):
floats            3 non-null float64
integers          3 non-null int64
ints with None    2 non-null float64
text              3 non-null object
dtypes: float64(2), int64(1), object(1)
memory usage: 120.0+ bytes

要获取 DataFrame 的内存使用情况:

>>> df.info(memory_usage='deep')
<class 'pandas.core.frame.DataFrame'>
Int64Index: 3 entries, 0 to 2
Data columns (total 4 columns):
floats            3 non-null float64
integers          3 non-null int64
ints with None    2 non-null float64
text              3 non-null object
dtypes: float64(2), int64(1), object(1)
memory usage: 234.0 bytes