0

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!

The code output

The DataFrame

Henry Ecker
  • 34,399
  • 18
  • 41
  • 57
Angelism
  • 23
  • 5
  • Flats, terraced values are floats right? How can you create a pie chart using float values? – Bimsara Gayanga May 29 '21 at 16:08
  • Please include any relevant information [as text directly into your question](https://stackoverflow.com/editing-help), do not link or embed external images of source code or data. Images make it difficult to efficiently assist you as they cannot be copied and offer poor usability as they cannot be searched. See: [Why not upload images of code/errors when asking a question?](https://meta.stackoverflow.com/q/285551/15497888) – Henry Ecker May 29 '21 at 16:17
  • If you need assistance formatting a small sample of your DataFrame as a copyable piece of code for SO see [How to make good reproducible pandas examples](https://stackoverflow.com/q/20109391/15497888). – Henry Ecker May 29 '21 at 16:17
  • Thank you very much, I'll certainly have a look and make some amendments. – Angelism May 29 '21 at 16:46

1 Answers1

1

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 column names:

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:

enter image description here

Then repeat the same procedure for Angus.

Valdi_Bo
  • 30,023
  • 4
  • 23
  • 41
  • For some reason, when I try to run this program, I get an error saying Lenght mismatch: Expected axis has 19 elements, new values have 4 elements. However, when I renamed the columns with this code `df.rename(columns={'DetachedPrice':'Detached', 'SemiDetachedPrice':'SemiDetached', 'FlatPrice':'Flat', 'TerracedPrice':'Terraced'}, inplace=True)` I managed to get a pie chart but it also includes a slice for AveragePrice column. – Angelism May 30 '21 at 23:16
  • Apparently your DataFrame (the source to the plot) has some additional columns. Make sure it contains only 4 columns for your house categories. – Valdi_Bo May 31 '21 at 05:54