I am new to python and have a question regarding using pandas and matplotlib. I am parsing a CSV file and creating a bar chart using the column names. Depending on the CSV file I am parsing for that week, the column names may be different. For example, this week the column names may be "Wednesday", "Thursday", and "Sunday", but next week they may be other days of the week. My script works fine and I want it to be doing the same thing regardless. My question is how may I read in column names so that I don't have to manually specify the name and just have it find the column based on the location (column1, column2, column3,...".
NOTE: The first column is "Names" as shown in the script below and this part will always be the same, so it's okay to have the column name for this hard-coded in. So I would like to soft-code the column names for column 2, 3, and 4.
Here is a portion of my current script where I have to manually read in column names:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
files = "myfile.csv"
df = pd.read_csv(files)
names = df['Names'].values
x = np.arange(len(names))
w = 0.40
difference = df['Sunday'] - df['Thursday']
colors = ['Red' if d < -5 else 'Blue' for d in difference]
plt.bar(x-w, df['Wednesday'].values, width=w*0.7, label='Wednesday', color = "cyan")
plt.bar(x, df['Thursday'].values, width=w*0.7, label='Thursday', color = "green")
plt.bar(x+w, df['Sunday'].values, width=w*0.7, label='Sunday', color = colors)
...
Ideally, I want a program that looks like this:
import pandas as pd
import matplotlib.pyplot as plt
import numpy as np
files = "myfile.csv"
df = pd.read_csv(files)
names = df['Names'].values
x = np.arange(len(names))
w = 0.40
column2 = ...
column3 = ...
column4 = ...
difference = df[col4] - df[col3]
colors = ['Red' if d < -5 else 'Blue' for d in difference]
plt.bar(x-w, df[column2].values, width=w*0.7, label='column2', color = "cyan")
plt.bar(x, df[column3].values, width=w*0.7, label='column3', color = "green")
plt.bar(x+w, df[column4].values, width=w*0.7, label='column4', color = colors)
...
For a more clear understanding, here is a sample of what the CSV file looks like:
Name Monday Wednesday Saturday
Derick 45 60 52
Jenna 56 87 89
Lisa 78 93 76
Harry 98 84 79