Pandas DataFrame Method

Rate this post


Before any data manipulation can occur, three (3) new libraries will require installation.

  • The Pandas library enables access to/from a DataFrame.
  • The Matplotlib library displays a visual graph of a plotted dataset.
  • The Scipy library allows users to manipulate and visualize the data.

To install these libraries, navigate to an IDE terminal. At the command prompt ($), execute the code below. For the terminal used in this example, the command prompt is a dollar sign ($). Your terminal prompt may be different.

$ pip install pandas

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install matplotlib

Hit the <Enter> key on the keyboard to start the installation process.

$ pip install scipy

Hit the <Enter> key on the keyboard to start the installation process.

If the installations were successful, a message displays in the terminal indicating the same.

Feel free to view the PyCharm installation guide for the required libraries.

Add the following code to the top of each code snippet. This snippet will allow the code in this article to run error-free.

import pandas as pd
import matplotlib.pyplot as plt
import scipy

DataFrame Vertical Bar

The method is a Vertical Bar chart representing data with rectangular bars. The lengths (height) of these bars define the values they represent.

The syntax for this method is as follows:, y=None, **kwargs)
xThis parameter determines the coordinates for the x-axis. Default is the index.
yThis parameter determines the coordinates for the y axis. Default is columns.
colorThis parameter can be a string, an array, or a dictionary to signify color(s).
– A single color can be specified by name, RGB or RGBA
– A color sequence specified by name, RGB, or RGBA.
– A dict of the form (col name/color) so each column is colored differently.
**kwargsAdditional keywords are outlined above in the plot() method.

Rivers Clothing would like a Vertical Bar chart of its sales based on sizes sold over the past six (6) months.

df = pd.DataFrame({'Tops':   [40, 12, 10, 26, 36],
                   'Pants':  [19, 8, 30, 21, 38],
                   'Coats':  [10, 10, 42, 17, 37]}, 
                    index=['XS', 'S', 'M', 'L', 'XL'])
ax = plt.gca()
plt.title('Rivers Clothing - Sold')


The buttons on the bottom left can be used to further manipulate the chart.

πŸ’‘Β Note: Another way to create this chart is with the plot() method and the kind parameter set to the 'bar' option.

More Pandas DataFrame Methods

Feel free to learn more about the previous and next pandas DataFrame methods (alphabetically) here:

Also, check out the full cheat sheet overview of all Pandas DataFrame methods.