Posted By: Anonymous
I suppose to create a pie chart displaying the prices of the Detached, Semi-Detached, Flats, and Terraced columns for the Aberdeenshire and Angus region from a Pandas DataFrame.
The task from the paper: "Show the split in sales value between flats, terraced, detached and semi
detached properties in the Aberdeenshire region using a pie chart, then show
this for Angus."
Here is what I did:
import pandas as pd from matplotlib import pyplot as plt import matplotlib.pyplot as plt df = pd.read_csv ('C:/Users/user/AppData/Local/Programs/Python/Python39/Scripts/uk_hpi_dataset_2021_01.csv') sales_value = df.loc[df['RegionName'] == 'Aberdeenshire', ['DetachedPrice', 'SemiDetachedPrice', 'FlatPrice', 'TerracedPrice']] sales_value.plot(sales_value, kind='pie', subplots=True)
However, when I try to print the result, I get four weird-looking individual lists with numbers and color codes – which I assume they meant to represent all the ‘Aberdeenshire’ rows in the ‘RegionName’ column – but not a Pie Chart. I might be close to the answer but can anyone give me a hand in turning this into an actual Pie Chart?
Any answers will be highly appreciated!
You titled your post as "How to create a Pie Chart…", so I assume that
you want to create a single plot, presenting e.g. average prices
in your 4 house categories.
As the source DataFrame I took:
Date RegionName DetachedPrice SemiDetachedPrice FlatPrice TerracedPrice 0 01/01/2004 Aberdeenshire 122490.0641 70563.15784 60200.15 80300.22 1 01/02/2004 Aberdeenshire 121280.8840 70804.42408 60300.15 80500.22 2 01/03/2004 Aberdeenshire 123395.4269 72689.07253 60400.15 80800.22 3 01/01/2004 SomeOther 123500.4000 72000.07000 70400.15 77000.22
After you created sales_value, it is a good idea to drop Price from
sales_value.columns = ['Detached', 'SemiDetached', 'Flat', 'Terraced']
Then, first compute the mean prices and only after that
create the plot:
sales_value.mean().plot.pie(ylabel='', title='Mean house prices');
subplots in my opinion is not needed, as you want a single plot.
ylabel='' is required to suppress ‘None’ y label printed otherwise.
For my test data I got:
Then repeat the same procedure for Angus.