Thanks to theidioms.com

Learn Pandas for Data Science (Course IV)

Learn Pandas for Data Science (Course IV)

Visualizing data using Pandas

Pandas uses another popular python module matplotlib for visualization. Usually, visualization is done from data that is stored as pandas objects. This chapter covers a few simple examples showing visualizing data using pandas.

Line Plot in Pandas

A line plot of data can be made using plot() function of DataFrame object as shown in the example below:

# Making necessary imports
import pandas as pd
import matplotlib.pyplot as plt

# Load data into dataframe
data = pd.read_csv("https://github.com/plotly/datasets/raw/master/2014_apple_stock.csv", index_col=0)

# Plot the line from dataframe specifying x and y labels
data.plot()
plt.xlabel('time')
plt.ylabel('value')

visualizing data using pandas

Scatter Plot in Pandas

A scatter plot of data can be made using plot.scatter() function of DataFrame object as shown in the example below:

# Making necessary imports
import pandas as pd

# Loading the data into dataframe
df = pd.DataFrame([[5.1, 3.5, 0], [4.9, 3.0, 0], [7.0, 3.2, 1],
                   [6.4, 3.2, 1], [5.9, 3.0, 2]],
                  columns=['length', 'width', 'species'])

# Making the scatter plot
ax1 = df.plot.scatter(x='length',
                      y='width',
                      c='DarkBlue')
Visualizing data using Pandas

Histogram Plot in Pandas

A histogram plot of data can be made using plot.hist() function of DataFrame object as shown in the example below:

'''
This histogram below shows the distribution of each value when we:
- draw one dice 9000 times 
- draw two dices 9000 time and sum the result
'''

# Making necessary imports
import pandas as pd
import numpy as np

# Create dataframe
df = pd.DataFrame(
    np.random.randint(1, 7, 9000),
    columns = ['one'])

# Add new column to dataframe by using addition 
df['two'] = df['one'] + np.random.randint(1, 7, 9000)
ax = df.plot.hist(bins=12, alpha=0.8)

Visualizing data using Pandas

Bar Graph in Pandas

A bar plot of data can be made using pandas.DataFrame.plot.bar() function of DataFrame object as shown in the example below:

# Making necessary imports
import pandas as pd
import numpy as np

# Loading the data into dataframe
df = pd.DataFrame({'Cake_items':['kitkat', 'Unicorn', 'Chocolateroll', 'Barbiedoll','Doraemon'],
                   'Sales':[235,554,582,695,545]})

# Making the bar plot
ax = df.plot.bar(x='Cake_items', y='Sales', rot=0)

Visualizing data using Pandas

Saving plots in Pandas

Pandas plots can be easily saved using savfig() function of matplotlib as shown in the example below:

# plot made using pandas
ax = df.plot()

# getting the figure from plot
fig = ax.get_figure()

# saving plot as png image
fig.savefig('plot.png') # This saves to current directory; you can also specify some other paths

With this, we have come to the end of our Pandas for Data Science Course. We hope that this course helped you as a stepping stone towards your Data Science journey. Also, if you have any questions or feedback, please feel free to let us know in the comment section. Feel free to browse for other courses on our platform to continue your journey towards Data Science with Python.

Leave your thought here

Your email address will not be published. Required fields are marked *

Close Bitnami banner
Bitnami