From 14ba0bb0eae37475c1b3e51091f5033766821308 Mon Sep 17 00:00:00 2001 From: s45147 Date: Sat, 23 Dec 2017 19:18:52 +0100 Subject: [PATCH] task02 ok --- labs06/task02.py | 70 +++++++++++++++++++++++++++++++++++++++--------- 1 file changed, 58 insertions(+), 12 deletions(-) diff --git a/labs06/task02.py b/labs06/task02.py index 9d96016..095b0bd 100755 --- a/labs06/task02.py +++ b/labs06/task02.py @@ -1,14 +1,24 @@ #!/usr/bin/env python # -*- coding: utf-8 -*- +import pandas as pd +import matplotlib.pyplot as plt + def wczytaj_dane(): - pass + csv_data = pd.read_csv('mieszkania.csv', index_col='Id') + return pd.DataFrame(csv_data) + def most_common_room_number(dane): - pass + + dane_agg = dane["Rooms"].value_counts() + return dane_agg.index.tolist()[0] + def cheapest_flats(dane, n): - pass + dane_cheapest = dane.sort_values(by=["Expected"])[:n] + return dane_cheapest + def find_borough(desc): dzielnice = ['Stare Miasto', @@ -19,36 +29,72 @@ def find_borough(desc): 'Winogrady', 'Miłostowo', 'Dębiec'] - pass + first = "" + found = False + for dz in dzielnice: + if dz in desc: + first = dz + found = True + break + + if not found: + return 'Inne' + else: + return first def add_borough(dane): - pass + dane['Borough'] = dane['Location'].map(lambda loc: find_borough(loc)) + def write_plot(dane, filename): - pass + + dane['Borough'].value_counts().plot(kind='bar', figsize = (10, 10)) + plt.savefig(filename) + def mean_price(dane, room_number): - pass + ff = dane[dane['Rooms'] == room_number] + return ff['Expected'].mean() + def find_13(dane): - pass + + ff = dane[dane['Floor'] == 13] + return ff['Borough'].unique() + def find_best_flats(dane): - pass + + bf = dane[(dane['Rooms'] == 3) & (dane['Floor'] == 1) & (dane['Borough'] == 'Winogrady')] + return bf + def main(): + dane = wczytaj_dane() + add_borough(dane) print(dane[:5]) + print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}" .format(most_common_room_number(dane))) print("{} to najłądniejsza dzielnica w Poznaniu." - .format(find_borough("Grunwald i Jeżyce")))) + .format(find_borough("Grunwald i Jeżyce"))) print("Średnia cena mieszkania 3-pokojowego, to: {}" - .format(mean_price(dane, 3))) + .format(mean_price(dane, 3))) + + print("Dzielnice z mieszkaniami na 13 piętrze, to: {}" + .format(find_13(dane))) + + ile = 10 + print("Najtańsze oferty mieszkań, to: {}" + .format(cheapest_flats(dane, ile))) + + write_plot(dane, 'mieszkania_plot.png') + if __name__ == "__main__": - main() + main() \ No newline at end of file