To query the memory usage of a Pandas DataFrame in Python, you can use the memory_usage() method. This method returns the memory usage of each column of the DataFrame.
Here's an example:
import pandas as pd# create a sample DataFramedf = pd.DataFrame({'A': [1, 2, 3], 'B': [4, 5, 6], 'C': ['a', 'b', 'c']})# get the memory usage of the DataFramememory_usage = df.memory_usage(deep=True)print(memory_usage) |
Output:
Index 128A 24B 24C 156dtype: int64 |
The memory_usage() method returns a Pandas Series object that shows the memory usage of each column of the DataFrame. The deep=True argument tells Pandas to include the memory usage of objects within each column (e.g. if a column contains strings, it will include the memory usage of each string).
Note that the index is also included in the memory usage calculation. In this example, the index takes up 128 bytes of memory.
You can sum the memory usage to get the total memory usage of the DataFrame:
total_memory_usage = memory_usage.sum()print(total_memory_usage) |
Output:
|