Skip to content

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.
  • Labs icon Lab
  • A Cloud Guru
Google Cloud Platform icon
Labs

Use pandas DataFrames on Excel Data

In this lab, we take a .csv file and create an Excel workbook out of it using pandas. The pdf of the notebook for this lab is [here.](https://github.com/linuxacademy/content-python-for-database-and-reporting/blob/master/pdf/hol_4_2_l_solution.pdf)

Google Cloud Platform icon
Labs

Path Info

Level
Clock icon Beginner
Duration
Clock icon 30m
Published
Clock icon Mar 13, 2020

Contact sales

By filling out this form and clicking submit, you acknowledge our privacy policy.

Table of Contents

  1. Challenge

    Start Jupyter Notebook Server and Access on Your Local Machine

    Connecting to the Jupyter Notebook Server

    Make sure that you have activated the virtual environment!

    1. Use the following to activate the virtual environment:
    conda activate base
    
    1. To start the server, run the following:
    python get_notebook_token.py
    

    This is a simple script that starts the Jupyter notebook server and sets it to continue to run outside of the terminal.

    On the terminal is a token, please copy this and save it to a text file on your local machine.

    On Your Local Machine

    1. In a terminal window, enter the following:
    ssh -N -L localhost:8087:localhost:8086 cloud_user@<the public IP address of the Playground server>
    

    It will ask you for your password; this is the password you use to login to the Playground remote server.

    Leave this terminal open, it will appear nothing has happened, but it must remain open while you use the Jupyter Notebook server in this session.

    1. In the browser of your choice, enter the following address:

    http://localhost:8087

    This will open a Jupyter Notebook site that asks for the token you copied from the remote server.

  2. Challenge

    Read the File Into a DataFrame

    # open file for reading
    f = open('dow_jones_index.data')
    
    # print the first two lines
    print(f.readline())
    print(f.readline())
    
    f.close()
    

    It appears the file is CSV. Read the file into a dataframe.

    import pandas as pd
    
    stock_df = pd.read_csv('dow_jones_index.data')
                           
    stock_df.head()
    
  3. Challenge

    Create the Excel Workbook

    Create a dataframe for each of the requested stocks

    ge_df = stock_df[stock_df.stock=='GE']
    ibm_df = stock_df[stock_df.stock=='IBM']
    krft_df = stock_df[stock_df.stock=='KRFT']
    

    Write the Excel file

    with pd.ExcelWriter('stocks.xlsx') as writer:  
        ge_df.to_excel(writer, sheet_name='GE')
        ibm_df.to_excel(writer, sheet_name='IBM')
        krft_df.to_excel(writer, sheet_name='KRFT')
    
    
  4. Challenge

    Check the Excel Workbook Contains the Requested Data

    Load the file into an ordered dict dataframe and then check that each worksheet is populated.

    my_stock_df = pd.read_excel('stocks.xlsx', sheet_name=None)  
    
    my_stock_df.keys()
    
    my_stock_df['GE']
    
    my_stock_df['IBM']
    
    my_stock_df['KRFT']
    

The Cloud Content team comprises subject matter experts hyper focused on services offered by the leading cloud vendors (AWS, GCP, and Azure), as well as cloud-related technologies such as Linux and DevOps. The team is thrilled to share their knowledge to help you build modern tech solutions from the ground up, secure and optimize your environments, and so much more!

What's a lab?

Hands-on Labs are real environments created by industry experts to help you learn. These environments help you gain knowledge and experience, practice without compromising your system, test without risk, destroy without fear, and let you learn from your mistakes. Hands-on Labs: practice your skills before delivering in the real world.

Provided environment for hands-on practice

We will provide the credentials and environment necessary for you to practice right within your browser.

Guided walkthrough

Follow along with the author’s guided walkthrough and build something new in your provided environment!

Did you know?

On average, you retain 75% more of your learning if you get time for practice.

Start learning by doing today

View Plans