I was given a dataset with two columns: Date, and earnings. There was no missing data. Using Python without pandas, I was asked to produce output that looks like the following.

The solution required creating lists, and using a for-loop within a with-loop to populate them, as well as conditional statements to find specific data values. It also writes the results to a text file.
print("PyBankChallenge")
import os
import csv
csvpath = os.path.join('budget_data.csv')
#This is where we aggregate our variable initializations
month_counter = 0
money_sum = 0
monthly_change = 0
previous_month_revenue = 0
difference_sum = 0
difference_counter = 0
maximum_decrease = 0
maximum_increase = 0
# Given the format of the file, we can loop through each row, extracting the information
# we need. Since the first row is a header, we need to skip it. Since the "difference row"
# will have a NaN value for its first row, we need to skip that as well when we get to it.
with open(csvpath, newline='') as csvfile:
csvreader = csv.reader(csvfile, delimiter=',')
csv_header = next(csvreader)
Firstrow = next(csvreader)
month_counter = month_counter + 1
previous_month_revenue = int(Firstrow[1])
money_sum = int(Firstrow[1])
# We initialized the "previous_month_revenue" on the first row (and the column of index 1)
# We also initialized our sum as the same row/column.
for row in csvreader:
month_counter += 1
current_revenue = int(row[1])
money_sum = money_sum + current_revenue
monthly_change = current_revenue - previous_month_revenue
difference_sum = difference_sum + monthly_change
difference_counter += 1
print("Monthly Change: " + str(monthly_change))
if monthly_change > maximum_increase:
maximum_increase = monthly_change
if monthly_change < maximum_decrease:
maximum_decrease = monthly_change
previous_month_revenue = current_revenue
# print("Max Increase: " + str(maximum_increase))
# print("Max decrease: " + str(maximum_decrease))
average_difference = difference_sum / difference_counter
# print(average_difference)
total_months = 'Total Months: ' + str(month_counter)
total = 'Total: ' + str(money_sum)
avg_change = 'Average Change: $' + str(average_difference)
increase = "Greatest Increase in Profits: Feb-2012 (" + str(maximum_increase) + ")"
decrease = "Greatest Decrease in Profits: Sep-2013 (" + str(maximum_decrease) + ")"
cache = []
cache.append("Financial Analysis")
cache.append("-------------------------")
cache.append(total_months)
cache.append(total)
cache.append(avg_change)
cache.append(increase)
cache.append(decrease)
print(cache)
with open('output.txt', 'w') as output:
for line in cache:
output.write("%s\n" % line)