Plotting a bar plot with DataFrame

For my class demo on 12 Jul 2018
## Concat
%matplotlib inline
import pandas as pd
import matplotlib.ticker as ticker
import matplotlib.pyplot as plt
import numpy as np

d1 = pd.read_csv("data/ura-private-district9.csv",skiprows=2)
d2 = pd.read_csv("data/ura-private-district3.csv",skiprows=2)

d = pd.concat([d1,d2])
d = d.rename(index=str,
            columns={"S/N":"SerialNumber","Price ($)":"Price", 
            "Project Name": "Project"})

d["SerialNumber"] = pd.to_numeric(d["SerialNumber"], errors="coerce")
d = d[~pd.isnull(d.Price)]
d["Unit Price ($psf)"] = pd.to_numeric(d["Unit Price ($psf)"], errors="coerce")
d["Date of Sale"] = pd.to_datetime(d["Date of Sale"], errors="coerce")

projects = d.groupby("Project")
medianpricebyproject = projects['Price'].median()
medianpricebyproject= pd.DataFrame(medianpricebyproject).sort_values(by=["Price"],
                                   ascending=False)
remover1 = lambda x: x.strip(' ').replace("\n","")
d_subset_01 = medianpricebyproject[medianpricebyproject.Price>5000000]
d_subset_01 = d_subset_01.sort_values(by=["Price"],ascending=False)

ax = d_subset_01.plot(kind="bar",figsize=(20,10))

from matplotlib.ticker import FuncFormatter
def millions(x, pos):
    'The two args are the value and tick position'
    return '$%1.1fM' % (x*1e-6)

formatter = FuncFormatter(millions)

ax.yaxis.set_major_formatter(formatter)

This is how it looks like:

Comments

Popular posts from this blog

How to create an organizational chart in your webpage using Google Organization Chart Tools

How to add Diggit, Technorati, Del.icio.us, Stumbleupon, Reddit and pInterest buttons to your Blogger posts