forked from tdwojak/Python2018
94 lines
2.3 KiB
Python
94 lines
2.3 KiB
Python
#!/usr/bin/env python
|
|
# -*- coding: utf-8 -*-
|
|
import pandas as pd
|
|
import matplotlib.pyplot as plt
|
|
import numpy as np
|
|
|
|
|
|
def wczytaj_dane():
|
|
return pd.read_csv('mieszkania.csv')
|
|
|
|
|
|
def most_common_room_number(dane):
|
|
return dane.Rooms.value_counts().index[0]
|
|
|
|
|
|
def cheapest_flats(dane, n):
|
|
return dane.sort_values(by=['Expected'], ascending=False).head(n)
|
|
|
|
|
|
def find_borough(desc):
|
|
dzielnice = ['Stare Miasto',
|
|
'Wilda',
|
|
'Jeżyce',
|
|
'Rataje',
|
|
'Piątkowo',
|
|
'Winogrady',
|
|
'Miłostowo',
|
|
'Dębiec']
|
|
|
|
common_boroughs = list(set(desc.split()).intersection(dzielnice))
|
|
if not common_boroughs:
|
|
return "Inne"
|
|
else:
|
|
return str(common_boroughs[0])
|
|
|
|
|
|
def add_borough(dane):
|
|
dane['Borough'] = dane.Location.apply(lambda el: find_borough(str(el)))
|
|
|
|
|
|
def write_plot(dane, filename):
|
|
column_names = list(dane.Borough.value_counts().index)
|
|
x_pos = np.arange(len(column_names))
|
|
y_pos = list(dane.Borough.value_counts())
|
|
|
|
plt.bar(x_pos, y_pos, align='center')
|
|
plt.xticks(x_pos, column_names)
|
|
plt.xlabel('Dzielnica')
|
|
plt.ylabel('Liczba ogłoszeń')
|
|
plt.title('Liczba ogłoszeń mieszkaniowych z podziałem na dzielnice')
|
|
|
|
img = plt.gcf()
|
|
img.savefig(filename)
|
|
|
|
|
|
def mean_price(dane, room_number):
|
|
return dane.Expected[dane.Rooms == room_number].mean()
|
|
|
|
|
|
def find_13(dane):
|
|
return dane.Borough[dane.Floor == 13].unique()
|
|
|
|
|
|
def find_best_flats(dane):
|
|
return dane[(dane.Borough == 'Winogrady') & (dane.Rooms == 3) & (dane.Floor == 1)]
|
|
|
|
|
|
def main():
|
|
dane = wczytaj_dane()
|
|
print(dane[:5])
|
|
|
|
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}"
|
|
.format(most_common_room_number(dane)))
|
|
|
|
print("{} to najładniejsza dzielnica w Poznaniu."
|
|
.format(find_borough("Grunwald i Jeżyce")))
|
|
|
|
print("Średnia cena mieszkania 3-pokojowego, to: {}"
|
|
.format(mean_price(dane, 3)))
|
|
|
|
add_borough(dane)
|
|
|
|
print("Lista dzielnic, które zawierają ofertę mieszkania na 13 piętrze: {}"
|
|
.format(', '.join(find_13(dane))))
|
|
|
|
write_plot(dane, "test")
|
|
|
|
# print("Oferty mieszkań, które znajdują się na Winogradach, mają 3 pokoje i są na 1 piętrze:")
|
|
# print(find_best_flats(dane))
|
|
|
|
|
|
if __name__ == "__main__":
|
|
main()
|