Home »
Python »
Python Programs
How to exclude a few columns from a DataFrame plot?
Given a pandas dataframe plot, we have to exclude a few columns from it.
By Pranit Sharma Last updated : October 06, 2023
Pandas is a special tool that allows us to perform complex manipulations of data effectively and efficiently. Inside pandas, we mostly deal with a dataset in the form of DataFrame. DataFrames are 2-dimensional data structures in pandas. DataFrames consist of rows, columns, and data.
Problem statement
We are given the data frame with multiple columns, several of which hold data that is not suitable for plotting.
Excluding a few columns from a DataFrame plot
When we use DataFrame.hist(), it throws an error on those, so we need to specify those columns should be excluded from the plotting. The DataFrame.hist() method is used to plot the data frame to visualize the data. Data visualization is a process of representing the data in the form of graphs, plots, and other pictorial formats for a better understanding of data and effective analysis of data.
To exclude a few columns from the dataframe plot we will simply use the data frame drop method inside which we will pass the required columns and then we will apply the DataFrame.hist() method.
Let us understand with the help of an example,
Python program to exclude a few columns from a DataFrame plot
# Importing pandas package
import pandas as pd
# Importing numpy package
import numpy as np
# Creating a dictionary
d = {
'Length': [2.7, 8.7, 3.4, 2.4, 1.9],
'Breadth': [4.24, 2.67, 7.6, 7.1, 4.9],
'foo':['bar','zoo','foo','bar','foo']
}
# Creating a DataFrame
df = pd.DataFrame(d)
# Display original DataFrame
print("Original DataFrame:\n",df,"\n")
# Dropping column foo and applying hist
res = df.drop(['foo'], axis=1).hist()
# Display result
print("Result:\n",res)
Output
The output of the above program is:
Python Pandas Programs »