Very new to coding, so please excuse the lack of finesse. I will try to describe my problem as best as I can.
I have a tabular list of 'City_names' and 'Year_spending', and would like to create plots of spending versus time (Year), color coded by city_names. How would I best approach this in Pandas?
This is the current format of my table:
City_names | Year_2000_spending | Year_2002_spending | Year_2003_spending |
---|---|---|---|
City 1 | $1 | $5 | $1 |
City 2 | $8 | $7 | $7 |
City 3 | $5 | $3 | $9 |
CodePudding user response:
You could start with this. This gives a bar graph of spending versus time (Year), color coded by city_names, as shown below.
import matplotlib.pyplot as plt
import pandas as pd
df = pd.DataFrame([['City 1', '$1', '$5', '$1'],['City 2', '$8', '$7', '$7'],['City 3', '$5', '$3', '$9']], columns=['City_names', 'Year_2000_spending', 'Year_2002_spending', 'Year_2003_spending'])
df=df.replace('\$','',regex=True)
df[['Year_2000_spending', 'Year_2002_spending', 'Year_2003_spending']] = df[['Year_2000_spending', 'Year_2002_spending', 'Year_2003_spending']].apply(pd.to_numeric)
df = df.set_index('City_names')
df = df.T.reset_index()
df.plot.bar(x='index')
plt.xticks(rotation=0, ha='right')
plt.show()