1
0
forked from tdwojak/Python2017
Python2017/labs06/task02.py
2017-12-23 19:18:52 +01:00

100 lines
2.1 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import matplotlib.pyplot as plt
def wczytaj_dane():
csv_data = pd.read_csv('mieszkania.csv', index_col='Id')
return pd.DataFrame(csv_data)
def most_common_room_number(dane):
dane_agg = dane["Rooms"].value_counts()
return dane_agg.index.tolist()[0]
def cheapest_flats(dane, n):
dane_cheapest = dane.sort_values(by=["Expected"])[:n]
return dane_cheapest
def find_borough(desc):
dzielnice = ['Stare Miasto',
'Wilda',
'Jeżyce',
'Rataje',
'Piątkowo',
'Winogrady',
'Miłostowo',
'Dębiec']
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):
dane['Borough'] = dane['Location'].map(lambda loc: find_borough(loc))
def write_plot(dane, filename):
dane['Borough'].value_counts().plot(kind='bar', figsize = (10, 10))
plt.savefig(filename)
def mean_price(dane, room_number):
ff = dane[dane['Rooms'] == room_number]
return ff['Expected'].mean()
def find_13(dane):
ff = dane[dane['Floor'] == 13]
return ff['Borough'].unique()
def find_best_flats(dane):
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")))
print("Średnia cena mieszkania 3-pokojowego, to: {}"
.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()