Now to just add on to our knowledge of Pandas we practiced using them a little more by making a more logical code with single script with input/output.
I think this was more focussed on object oriented programming in python. I learnt more about this as I never knew about this before and I learnt how this was different from languages like java where here we have variables like self or commands like __init__ and such. This is my code: import pandas as pd class EnergyAnalysis: def __init__(self, csv, column, column2): self.csv = pd.read_csv(csv) self.csvName = csv self.column = column self.column2 = column2 def setCsv(self, csv): self.csv = pd.read_csv(csv) def setColumn(self, column): self.column = column def setColumn2(self, column2): self.column2 = column2 def getCsv(self): return self.csvName def getColumn(self): return self.column def getColumn2(self): return self.column2 def sumCompare(self): column1sum = self.csv[self.column].sum() column2sum = self.csv[self.column2].sum() result="" if column1sum > column2sum: columnDifference = column1sum-column2sum result="Sum of all values in " + self.column + " is " + str(column1sum) +". Sum of all values in " + \ self.column2 + " is " + str(column2sum) + ". Sum of all values in " + self.column + \ " is greater than the sum of all values in " + self.column2 + ". " + self.column + \ " is greater than sum of " + self.column2 + " by " + str(columnDifference) elif column2sum > column1sum: columnDifference = column2sum-column1sum result="Sum of all values in " + self.column + " is " + str(column1sum) + ". Sum of all values in " +\ self.column2 + " is " + str(column2sum) + ". Sum of all values in " + self.column2 + \ " is greater than the sum of all values in " + self.column + ". " + self.column2 + \ "is greater than sum of " + self.column + " by " + str(columnDifference) elif column1sum == column2sum: result="Sum of all values in " + self.column + " is " + str(column1sum) +". Sum of all values in " + \ self.column2 + " is " + str(column2sum) + ". Sum of all values in " + self.column + \ " is equal to the sum of all values in " + self.column2 + "." return result def avgCompare(self): column1avg = self.csv[self.column].mean() column2avg = self.csv[self.column2].mean() if column1avg > column2avg: columnDifference = column1avg-column2avg return "Average of " + self.column + " is " + str(column1avg) + " and average of " + self.column2 + \ " is " + str(column2avg) + ". Average of " + self.column + " is greater than " + self.column2 + \ " by " + str(columnDifference) elif column2avg > column1avg: columnDifference = column2avg - column1avg return "Average of " + self.column + " is " + str(column1avg) + " and average of " + self.column2 + \ " is " + str(column2avg) + ". Average of " + self.column2 + " is greater than " + self.column + \ " by " + str(columnDifference) elif column2avg == column1avg: return "Average of " + self.column + " is " + str(column1avg) + " and average of " + self.column2 + \ " is " + str(column2avg) + ". Average of " + self.column2 + " is equal to " + self.column def totalCompare(self, filename): file = open((filename+".txt"),"w") file.write(self.sumCompare()) file.write("") file.write(self.avgCompare()) file.close() print("Exiting totalCompare") test = EnergyAnalysis("2016 Q1Q4 - Electrical.csv", "HS-Kitchen Emergency (kWh)", "HS Main (kWh)") test.totalCompare("test1") print("Done") The output is in a .txt file where it compares the two columns
0 Comments
Leave a Reply. |
AuthorHi! My name is Ayush Zenith! I am currently a senior at Andover High School. I have been part of the Energize Andover Program since June, 2017 (8th grade). I have since been working on improving my knowledge in programming and working on writing better applications in order to save and conserve resources in buildings... Archives
November 2020
Categories |