[go: up one dir, main page]

0% found this document useful (0 votes)
18 views18 pages

Lesson 24 Notes - Graphing From A Dataframe

Lesson 24 Notes_ Graphing from a Dataframe

Uploaded by

3037171
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
18 views18 pages

Lesson 24 Notes - Graphing From A Dataframe

Lesson 24 Notes_ Graphing from a Dataframe

Uploaded by

3037171
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as PDF, TXT or read online on Scribd
You are on page 1/ 18

Lesson 24 Notes

Graphing from a Dataframe


Warm Up
Imagine you had a CSV file called “Grades.csv”, which had columns for the student
names, ID#s, and grades.

Write the Python code to make a dataframe from “Grades.csv”, remove any
duplicate values, if any exist, and then find the mean, median, and mode of the
“Grades” column.
Combining Pandas and MatPlotLib
The final step we will take with all of this is to create data visualizations from the
columns of a dataset. The first step is to import both matplotlib and pandas
modules. Then we create a dataframe using the pandas module. Next, we isolate a
column, or columns, to work with. Finally, we can create the data visualization using
the functions in matplotlib and pandas.
Pandas Plot Function
The pandas module has a built in plot function. This function works for line graphs,
histograms, and scatter plots. We set up the data frame, then choose a column and
use the .plot( ) function to create the graph. By default the plot( ) function will create
a line graph. We will also need the matplotlib module to show the graph/chart.
Alternatively, you can use the matplotlib modules version of each of these as well.

The syntax is:


df[“COLUMN NAME”].plot( )
plt.show( )
Example:

In this example the x-axis is the ID# of the row. So in essence this graph shows the
maximum height of each dog breed in alphabetical order, which isn’t very useful.
Pandas Histogram
Inside the plot( ) function, we can change the kind attribute to be “hist”, which will
change the graph from a line graph to a histogram. See the example below:

*Note, we can also get a list from column and call plt.hist( ) and pass the list as the data.
Alternative Example:
Pandas Scatterplot
We can create a scatter plot by calling the plot( ) function directly on the data frame
and setting the x-axis and y-axis inside the plot function. We also have to set the
kind attribute to be “scatter”. See the example below:

*Note, like with the histogram, we can isolate the 2 columns and use
plt.scatter( ) and pass the lists from the columns as the data.
Alternative Example:
Pie Charts
Pandas does have a built in pie chart in the plot function. However, creating it is a
bit complicated. Instead we will use the functions we wrote in the frequency
analysis module to make the pie chart from the dataset.
Example:
Bar Charts
Like with pie charts, bar charts can be graphed from pandas’ plot function, but
again the process is complicated. It’s easy enough to use our frequency analysis
module to do the heavy lifting.
Example:
Try It:
What is the missing code?

A. df[“Minimum Weight”].plot(kind=”hist”)
B. df.plot(“hist”, “Minimum Weight”)
C. df[“Minimum Weight”].plot(type=”hist”)
D. None of these

Skip Ahead
Try It:
What is the missing code?

A. df.plot(kind=”bar”, “species”)
B. df.bar(“species”)
C. plt.bar(col_list)
D. plt.bar(uniq, freq)

Skip Ahead
Try It:
What is the missing code?

A. plt.plot(“Maximum Weight”, “Maximum Life Span”)


B. df.plot(kind=”scatter”, “Maximum Weight”, “Maximum Life
Span”)
C. df.scatter(“Maximum Weight”, “Maximum Life Span”)
D. plt.scatter(“Maximum Weight”, “Maximum Life Span”)

Skip Ahead
Try It:
What is the missing code?

A. df.pie(“islands”, autopct='%1.0f%%')
B. plt.pie(“islands”, autopct='%1.0f%%')
C. plt.pie(perc, labels=uniq,
autopct='%1.0f%%')
D. None of these

Skip Ahead
Wrap Up:
Write the Python code to make a data frame from the same “Grades.csv” file as in
the warm up and make a histogram of from the “Grades” column.

You might also like