How to obtain a concise summary of a DataFrame in Pandas
Overview
We use the info() function in the Pandas.DataFrame module to obtain a concise summary of a given DataFrame.
Syntax
The info() function takes the syntax shown below:
DataFrame.info(verbose=None, buf=None, max_cols=None, memory_usage=None, show_counts=None)
Syntax for the info() function
Parameters
The info() function takes the following optional parameter values:
verbose(optional): This parameter takes a Boolean value to indicate whether to print the full summary or not.buf(optional): This is a writable buffer to show where to send the output.memory_usage(optional): This takes a Boolean value indicating whether the total memory usage of the input DataFrame is to be displayed or not.show_counts(optional): This takes a Boolean value indicating whether or not to show non-null counts.
Return value
The info() function returns a summary of the DataFrame.
Example
Let's view the code example as shown below:
# A code to illustrate the info() function of a DataFrame objectimport pandas as pd# creating a list of objectsint_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]# creating a dataframe from the list of objectsdf = pd.DataFrame({"int_column": int_values, "text_column": text_values,"float_col": float_values})# getting the summary of the datafranea = df.info(verbose=True)print(a)
Explanation
- Line 3: We import the
pandaslibrary. - Line 6–8: We create a list of objects,
text_values,int_values, andfloat_values. - Line 11–12: We create a DataFrame using the list of objects we created by using
pandas.DataFrame(). The name of the DataFrame isdf. - Line 15: We obtain the summary of the DataFrame object. We assign the result to a variable,
a. - Line 17: We print the value of
a.