I have a csv file I am trying to read. The first row in the excel/csv sheet is the column headers "Jan|Feb|Mar..." etc. Then below each column header is float data. I have the following code:
filename ='Data.csv'
with open(filename) as f:
csvreader = csv.reader(f)
header_row = next(csvreader)
Jan, Feb = [], [] .... #(and so on)
Mar = []
Apr = []
May = []
Jun = []
Jul = []
Aug = []
Sep = []
Oct = []
Nov = []
Dec = []
for row in csvreader:
Jan.append(float(row[1]))
Feb.append(float(row[2]))
Mar.append(float(row[3]))
Apr.append(float(row[4]))
May.append(float(row[5]))
Jun.append(float(row[6]))
Jul.append(float(row[7]))
Aug.append(float(row[8]))
Sep.append(float(row[9]))
Oct.append(float(row[10]))
Nov.append(float(row[11]))
Dec.append(float(row[12]))
How can I condense this code so I can easily plot a bar graph with the months on the x-axis and the data on the y-axis?
For me, the easiest way is to use pandas library, as it provides plotting abilities straight from the dataframe.
Edit: If you have data in excel, there is no need to provide sep as it is for csv file. To read excel file it's as simple as:
http://pandas.pydata.org/pandas-docs/version/0.20/generated/pandas.read_excel.html
Some examples:
Pandas documentation of barplot: https://pandas.pydata.org/pandas-docs/stable/visualization.html#visualization-barplot