You are an AI assistant that aids users in performing data analysis using Python and Pandas to find information. 
 
There is the data  
You have the data melbourne_housing contains the following columns: ['Suburb', 'Address', 'Rooms', 'Type', 'Price', 'Method', 'SellerG', 'Date', 'Distance', 'Postcode', 'Bedroom2', 'Bathroom', 'Car', 'Landsize', 'BuildingArea', 'YearBuilt', 'CouncilArea', 'Lattitude', 'Longtitude', 'Regionname', 'Propertycount'].
--- The description for each column this data is:
Suburb: Name of the suburb where the property is located (Abbotsford, Airport West, Albert Park ...etc)
Address: Street address of the property (85 Turner St, 25 Bloomburg St, 5 Charles St ...etc)
Rooms: The number of rooms in the property
Type: Type of property (h: house, u: unit/apartment, t: townhouse)
Price: Sale price of the property (measured in dollars)
Method: Method of sale (S: Sold (property successfully sold), SP: Sold Prior (sold before auction), PI: Passed In (not sold at auction), VB: Vendor Bid (seller made a bid), and SA: Sold After (sold after auction))
SellerG: Name of the real estate agent or agency that facilitated the sale
Date: Date when the property sale was finalized (day/month/year)
Distance: Distance of the property from a significant central point (measured in kilometers)
Postcode: Postal code area of the property
Bedroom2: The number of bedrooms in the property
Bathroom: The number of bathrooms in the property
Car: The number of car parking spaces available with the property
Landsize: Size of the land on which the property is situated (measured in square meters)
BuildingArea: Size of the building itself (measured in square meters) 
YearBuilt: Year in which the property was constructed
CouncilArea: Local government area in which the property is located (Yarra, Moonee Valley, Port Phillip ...etc)
Latitude: Geographical latitude of the property
Longitude: Geographical longitude of the property
Regionname: Larger region or area that the property is part of (Northern Metropolitan, Western Metropolita, Southern Metropolitan ...etc)
Propertycount: The total number of properties in the suburb
---
Considering contents from the dataset and result types from user, you only need to generate codes and notations. 
Conversation begins:
[USER (data scientist)]: Hey Tapilot, I've got this "melbourne_housing" dataset, and I'm trying to figure out how many properties are in the affordable range of $300,000 to $600,000. Can you help me filter it down? Please generate and display a DataFrame to filter and visualize affordable housing options from the 'melbourne_housing' dataset, where the price range is between 300,000 and 600,000.

[YOU (AI assistant)]: Absolutely! Let's start by filtering the dataset based on that price range. Here's the code for that :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

affordable_housing = melbourne_housing[(melbourne_housing['Price'] >= 300000) & (melbourne_housing['Price'] <= 600000)]

print(affordable_housing)

'''

[USER (data scientist)]: Awesome, that's what I needed. now can you help me find the number of properties in this filtered dataset? Please load the 'melb_data.csv' dataset into a DataFrame and calculate the number of properties in it, storing the result as an integer in a pickle file.

[YOU (AI assistant)]: Sure thing! You can use this code to count the number of properties in the filtered dataset :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

number_of_properties = len(affordable_housing)

print(number_of_properties)

'''

[USER (data scientist)]: Hey, I've got this dataset called "affordable_housing" and I'm trying to figure out the top 10 suburbs and regions with the most affordable properties sold. Can you help me out with the code? Please generate two lists from the 'melbourne_housing' DataFrame to identify the top 10 most common suburbs and regions, then save these lists using pickle for future reference.

[YOU (AI assistant)]: Of course! Let's start by finding the top 10 suburbs and regions based on the number of affordable properties sold. We can use the value_counts() method to count how many times each unique value appears in the 'Suburb' and 'Regionname' columns. Here's what you can do :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

top_suburbs = affordable_housing['Suburb'].value_counts().head(10)
top_regions = affordable_housing['Regionname'].value_counts().head(10)
top_suburbs_list = top_suburbs.index.tolist()
top_regions_list = top_regions.index.tolist()

print(top_suburbs_list)

print(top_regions_list)

'''

[USER (data scientist)]: Hey, I've got this dataset of affordable houses, and I wanna find out which suburbs are the best for finding a good deal. Can you help me with that? Let's find the top 10 suburbs with the most affordable houses within 20 km of the CBD. Please filter the DataFrame 'melbourne_housing' to include only affordable housing options within 20 kilometers and save the resulting subset as a pickle file.

[YOU (AI assistant)]: Got it. Let's filter out the houses that are less than 20 km away :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

affordable_housing_within_20km = affordable_housing[affordable_housing['Distance'] <= 20]

print(affordable_housing_within_20km)

'''

[USER (data scientist)]: Cool, let's count the affordable houses in each suburb and pick the top 10. Please generate and display a dataframe showing the top 10 suburbs within 20 kilometers from Melbourne city center based on their frequency in the 'affordable_housing_within_20km' dataset, and serialize this subset for later use.

[YOU (AI assistant)]: Sure, I can help you with that. :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

top_suburbs_within_20km = affordable_housing_within_20km['Suburb'].value_counts().head(10)

print(affordable_housing_within_20km)

'''

[USER (data scientist)]: Hey Tapilot, I've got this "melbourne_housing" dataset, and I'm trying to figure out how many properties are in the affordable range of $300,000 to $600,000. Can you help me filter it down? Please generate and display a list of top suburbs within 20 kilometers, utilizing a DataFrame from the 'melbourne_housing' dataset.

[YOU (AI assistant)]: Sure, I can help you with that. :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

top_suburbs_within_20km_list = top_suburbs_within_20km.index.tolist()

print(top_suburbs_within_20km_list)

'''

[USER (data scientist)]: Hey, I have this Melbourne housing dataset and I need some help analyzing it. 
 
[YOU (AI assistant)]: Sure, I'd be happy to help! What kind of analysis are you looking to do? 
 
[USER (data scientist)]: I want to find out how many affordable houses, townhouses, and units were sold in the last 5 years. But first, let's make sure the 'Date' column is in the right format. Please load the 'melb_data.csv' file into a DataFrame and convert the 'Date' column to datetime format, then save the updated DataFrame to a pickle file.

[YOU (AI assistant)]: Alright, let's start by converting the 'Date' column to datetime format. You can use this code :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

melbourne_housing['Date'] = pd.to_datetime(melbourne_housing['Date'], dayfirst=True)

print(melbourne_housing)

'''

[USER (data scientist)]: Great, now let's filter the data to get only the past 5 years. Please filter the 'melbourne_housing' DataFrame to include only records from the past 5 years and save this subset as a pickle file.

[YOU (AI assistant)]: Sure, here's how you can filter the data to get the records from the past 5 years :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

past_5_years = melbourne_housing[melbourne_housing['Date'] >= (melbourne_housing['Date'].max() - pd.DateOffset(years=5))]

print(past_5_years)

'''

[USER (data scientist)]: Now, I want to find the affordable ones, with prices between 300,000 and 600,000. Please filter the 'melbourne_housing' DataFrame to select properties priced between $300,000 and $600,000 from the past 5 years, resulting in a new DataFrame and save it as a pickle file.

[YOU (AI assistant)]: Alright, you can filter the past 5 years data for affordable housing using this code :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

affordable_housing_past_5_years = past_5_years[(past_5_years['Price'] >= 300000) & (past_5_years['Price'] <= 600000)]

print(affordable_housing_past_5_years)

'''

[USER (data scientist)]: Perfect! Now, let's calculate the number of affordable houses sold in the past 5 years. Please generate and display a DataFrame and an integer to analyze the subset of affordable houses of type 'h' from the 'melbourne_housing' dataset over the past 5 years, and save both the filtered dataset and the count of affordable houses to files.

[YOU (AI assistant)]: To calculate the number of affordable houses sold, you can use this code :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

affordable_houses = affordable_housing_past_5_years[affordable_housing_past_5_years['Type'] == 'h']
number_of_affordable_houses = len(affordable_houses)

print(affordable_houses)

print(number_of_affordable_houses)

'''

[USER (data scientist)]: Thanks! Now, how about the number of affordable townhouses and units sold? Please generate a summary of the count of affordable townhouses and units sold, stored as integers, from the 'melbourne_housing' DataFrame.

[YOU (AI assistant)]: Sure, here's how you can calculate the number of affordable townhouses and units sold in the past 5 years :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

# Calculate the number of affordable townhouses sold
affordable_townhouses = affordable_housing_past_5_years[affordable_housing_past_5_years['Type'] == 't']
number_of_affordable_townhouses = len(affordable_townhouses)
 
# Calculate the number of affordable units sold
affordable_units = affordable_housing_past_5_years[affordable_housing_past_5_years['Type'] == 'u']
number_of_affordable_units = len(affordable_units)

print(number_of_affordable_townhouses)

print(number_of_affordable_units)

'''

[USER (data scientist)]: Hey Tapilot, I need your help with some data analysis. I have a dataset of affordable housing sales over the past 5 years, and I want to find out the annual number of affordable properties sold, the average price of affordable properties, and the percentage change in these metrics for each year. Can you help me with the code to achieve this?  Please extract the year from the 'Date' column and add it to the 'melbourne_housing' DataFrame, then display and save this modified DataFrame as a pickle file, generating a DataFrame and a pickle file as outputs.

[YOU (AI assistant)]: Sure, I can help you with that. First, let's create a new column 'Year' in your dataset to store the year of the sale date. Assuming your dataset is named affordable_housing_past_5_years and the sale date is stored in a column named 'Date', you can use the following code :
'''
import pandas as pd   
import pickle

# Load the dataset
melbourne_housing = pd.read_csv("melb_data.csv")

affordable_housing_past_5_years['Year'] = affordable_housing_past_5_years['Date'].dt.year

print(affordable_housing_past_5_years)

'''

[USER (data scientist)]: Great, now I want to calculate the annual number of affordable properties sold and the average price of affordable properties for each year. Please generate Series objects to summarize the annual number of affordable properties and their average prices over the past 5 years in the 'melbourne_housing' dataset.

My template of code snippet is:

---BEGIN CODE TEMPLATE---

import pandas as pd
import pickle

melbourne_housing = pd.read_csv("melb_data.csv") 

# YOUR SOLUTION BEGIN:
<code1>
[GENERATE YOUR CODE]
</code1>
# YOUR SOLUTION END:

print(annual_number_of_affordable_properties)

# save data
pickle.dump(annual_number_of_affordable_properties,open("./pred_result/annual_number_of_affordable_properties.pkl","wb"))

print(average_price_of_affordable_properties)

# save data
pickle.dump(average_price_of_affordable_properties,open("./pred_result/average_price_of_affordable_properties.pkl","wb"))

---END CODE TEMPLATE---

Please note that you have to generate the WHOLE python code instead of code segments based on the code snippet using Pandas library 2.0.3 version and Matplotlib library 3.7.4 version. You must keep all comments in code snippet unchanged.

[YOU (AI assistant)]: You can use the groupby method to group the data by year and then calculate the annual number of affordable properties sold and the average price. Here's the code: 
