DataFrames with Python

Step-by-step Python programming tutorial

Discover the intricate steps involved in creating a data frame from various lists in Python, a popular skill for data scraping and organization. Learn about the use of Python lists, the zip function, and the process of converting lists into a data frame for easier data analysis and manipulation.

Key Insights

  • The video focuses on creating a data frame from lists in Python, a commonly used programming language for data analysis.
  • A list is a popular Python container often used when gathering information, especially when using web scraping techniques.
  • Several lists are used as examples in the tutorial: a list of cities, a list of states, and a list of populations. These lists are then used to create a data frame.
  • A list called 'labels' is created to serve as labels for the future columns of the data frame: City, State, and Population.
  • The 'zip' function is used to return a list of tuples from the examples lists, which is then converted into a dictionary with keys corresponding to the labels.
  • The final step involves creating a variable name for the future data frame, passing the dictionary to it, and generating a data frame complete with the index, columns, and values.

Build a Python Script

  1. Define the Goal

    Single, specific outcome — avoid scope creep in scripts.

  2. Outline Functions

    Break logic into functions with clear inputs/outputs.

  3. Add Error Handling

    try/except for expected failures, raise for unexpected.

  4. Argparse for CLI

    argparse turns a script into a real command-line tool.

Play video

Video Transcription

Hi, my name is Art, and I teach Python at Noble Desktop. In this video, I'm going to show you how you can create a data frame from many lists. First, let's import Pandas as "pd".

Suppose we have a couple of lists. A list is a very popular Python container, and most of the time when you gather information (maybe you use web scraping techniques to gather information) you would use a list, and then you want to take those lists and convert them into a data frame.

Suppose we have a list of cities: New York, Chicago, Orlando, and Boston. Then we have a list of states: New York, Illinois, Florida, and Massachusetts. Then we have a population list (in millions): 10.5,4.5,1.6, and 2.5.

Python for Data Science Bootcamp: Live & Hands-on, In NYC or Online, Learn From Experts, Free Retake, Small Class Sizes,  1-on-1 Bonus Training. Named a Top Bootcamp by Forbes, Fortune, & Time Out. Noble Desktop. Learn More.

We have three Python lists. Now we want to convert and create the data frame. To do this, we might want to create one more list called labels. In this list, I want to place labels for my future columns: City, State, and Population.

Now we can compose them as another list called list_data. List_data is a list of lists. Now we can use the zip function. Zip always returns an object, so we can unpack this list using the list function.

We now have a list of tuples, and all we have to do is convert this to a dictionary. We can assign it the variable name data, and it will be a dictionary with keys: City, State, and Population.

Now, all we have to do is create a variable name for our future data frame (pd_data_frame) and pass the dictionary. Now you have this data frame with the index, columns, and all the values.

Topics

Learn more in these courses

How to Learn Python

Master Python with hands-on training. Python is a popular object-oriented programming language used for data science, machine learning, and web development. 

Yelp Facebook LinkedIn YouTube Twitter Instagram