Utilizing Pandas for Data Calculations and Predictions

Use pandas to create a data frame and perform vector operations to calculate predictions from attendance and concessions data.

Utilize pandas data frames to efficiently perform vector operations and accurately plot a best-fit line for data predictions. Learn how structured data analysis can refine predictive accuracy, even with limited datasets.

Key Insights

  • Create a pandas data frame named concessions data frame by passing a dictionary containing columns for attendance and concessions data to streamline vector operations.
  • Employ a pandas data frame to successfully calculate and visualize a best-fit line, enabling accurate predictions even with data outliers.
  • Demonstrate the effectiveness of structured data analysis using pandas, emphasizing the potential for improved predictive accuracy by expanding from limited datasets (e.g., attendance figures around 27,000 to 28,000) to larger datasets later in the course.

Note: These materials offer prospective students a preview of how our classes are structured. Students enrolled in this course will receive access to the full set of materials, including video lectures, project-based assignments, and instructor feedback.

Let's use a data frame to make more complex calculations here—to perform a vector operation on these values. So I'm going to call it `concessions_df`, and it will be the result of using pandas to create a data frame. We’re going to pass a dictionary to the DataFrame constructor with our values.

The first key will be `attendance`—that will be the name of our first column—and the value of that column will be the `attendance` list. Then we'll add another key called `concessions`, and the value of that will be our concessions Python list.

Now what we can do is perform those same vector operations, operating on every row in the column, by replacing this attendance list with `concessions_df['attendance']`. Let's see if this fixes our issue. Ah, that’s looking good.

There we are. Thanks, pandas. So this line is our best fit line.

And again, there are still some outliers, but if we are given a value like 27,000—or maybe 28,000—we could say, hey, it’s likely to be right around here, because this line should be fairly predictive. It would be even more accurate if we had more data. And we’ll do that later in this course.

Data Analytics Certificate: Live & Hands-on, In NYC or Online, 0% Financing, 1-on-1 Mentoring, Free Retake, Job Prep. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

We’ll have quite a lot of data. Let’s see how this prediction works.

Colin Jaffe

Colin Jaffe is a programmer, writer, and teacher with a passion for creative code, customizable computing environments, and simple puns. He loves teaching code, from the fundamentals of algorithmic thinking to the business logic and user flow of application building—he particularly enjoys teaching JavaScript, Python, API design, and front-end frameworks.

Colin has taught code to a diverse group of students since learning to code himself, including young men of color at All-Star Code, elementary school kids at The Coding Space, and marginalized groups at Pursuit. He also works as an instructor for Noble Desktop, where he teaches classes in the Full-Stack Web Development Certificate and the Data Science & AI Certificate.

Colin lives in Brooklyn with his wife, two kids, and many intricate board games.

More articles by Colin Jaffe

How to Learn Machine Learning

Master Machine Learning with Hands-on Training. Use Python to Make, Modify, and Test Your Own Machine Learning Models.

Yelp Facebook LinkedIn YouTube Twitter Instagram