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)
Successfully complete this lab by achieving the following learning objectives:
- 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!
- Use the following to activate the virtual environment:
conda activate base
- To start the server, run the following:
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
- 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.
- In the browser of your choice, enter the following address:
This will open a Jupyter Notebook site that asks for the token you copied from the remote server.
- 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()
- 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')
- 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()