70|PYTHON – Dart Gun Rates

Mark
Tue, 16 May 2023 18:34:39 +0000
Challenges

BYU Student Author: @Mark
Reviewers: @Jonathan_Weston, @Andrew
Estimated Time to Solve: 45 Minutes

We provide the solution to this challenge using:

Python

Need a program? Click here.

Overview
You work as a staff accountant (aka “Number Gunner”) at Big Bill’s, a company that sells high quality dart guns in many different countries worldwide. The company uses sales report software that doesn’t automatically update exchange rates each day. Big Bill’s management (aka “Top Gunners”) changes the company’s fixed exchange rates in the system occasionally, but not very consistently. Your fellow Number Gunner needs to figure out the sales discrepancies stemming from differences between the company’s fixed rates and the actual daily exchange rates in December. He knows that you are very skilled with your Python Pistols and can figure out how to scrape the daily rates for the currencies he needs, so he asks you to help him. He promises to present you with a gold star for excellence if you can write code (aka “The Secret Weapon”) that will pull the exchange rate for each unique currency and date combination in the spreadsheet he provides you. You salute and return to your station to create the perfect Secret Weapon…

Instructions

1. Import the packages you need to run your code. See “Suggestions and Hints” for the packages used in the solution. Also, change the float format to include 6 decimal places. This will ensure that your rates are very precise.

2. Import the data into a dataframe called “salesdf”

Create and use variables named “directory” and “filename” to import the data. This will allow the other Number Gunner to easily replace your directory and file name with his own going forward.

3. Create a new dataframe called “ratesdf” from salesdf (this will avoid issues later). Remove all columns you don’t need and all duplicates from ratesdf.

Remember that you need unique Currency Code and Shipped Date combinations. Also, are there any currencies that you don’t need to pull exchange rates for (ie, exchange rate = 1)? Does any of the data in the columns need to be transformed to work in step 4?

4. Iterate through the rows of the dataframe, using the data from the row to pull rates from the following website: EUR Historical Exchange Rates (Euro) - X-Rates

You may need to include headers (user agent) in order to access the website data.

Locate and replace the currency code and date within the website address to pull the rates. Make sure you match the formatting!

Be sure you are also pulling the correct rate. You want the rate that will translate to USD by multiplying the rate and the sales amount. Add the rates as a new column in the dataframe.

5. Merge ratesdf with salesdf to place the rates on the proper rows. Fill any nulls in the merged dataframe with “1”. Export the dataframe to an .xlsx file.

Data Files

Challenge70_Data.xlsx

Suggestions and Hints (click for more details)
Solution

spoiler

2 posts - 2 participants