Python2018/labs06/task02.py

73 lines
2.0 KiB
Python
Raw Normal View History

2018-06-03 10:04:16 +02:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2018-06-21 19:28:33 +02:00
import pandas as pd
from statistics import mode
import matplotlib.pyplot as plt
2018-06-03 10:04:16 +02:00
def wczytaj_dane():
2018-06-21 19:28:33 +02:00
r = pd.read_csv("mieszkania.csv")
dane = r.dropna(axis='columns')
return dane
2018-06-03 10:04:16 +02:00
def most_common_room_number(dane):
2018-06-21 19:28:33 +02:00
return mode(dane.Rooms)
2018-06-03 10:04:16 +02:00
def cheapest_flats(dane, n):
2018-06-22 15:59:02 +02:00
return dane.sort_values(by="Expected").head(n)
2018-06-03 10:04:16 +02:00
def find_borough(desc):
dzielnice = ['Stare Miasto',
'Wilda',
'Jeżyce',
'Rataje',
'Piątkowo',
'Winogrady',
'Miłostowo',
'Dębiec']
2018-06-21 19:28:33 +02:00
indeks = len(desc)
nazwa_dzielnicy = "Inne"
for dzielnica in dzielnice:
indeks_dzielnica = desc.find(dzielnica)
if indeks_dzielnica < indeks and indeks_dzielnica != -1:
indeks = indeks_dzielnica
nazwa_dzielnicy = dzielnica
return nazwa_dzielnicy
2018-06-03 10:04:16 +02:00
def add_borough(dane):
2018-06-21 19:28:33 +02:00
dane['Borough'] = dane['Location'].apply(find_borough)
return dane
2018-06-03 10:04:16 +02:00
def write_plot(dane, filename):
2018-06-21 19:28:33 +02:00
add_borough(dane)
dane_wykres = pd.Series(dane.Borough.value_counts())
dane_wykres.plot(x='Borough', y='Liczba ogłoszeń', kind = 'bar')
plt.savefig(filename, pad_inches=1, bbox_inches='tight')
2018-06-03 10:04:16 +02:00
def mean_price(dane, room_number):
2018-06-21 19:28:33 +02:00
return dane.Expected[(dane['Rooms'] == room_number)].mean()
2018-06-03 10:04:16 +02:00
def find_13(dane):
2018-06-21 19:28:33 +02:00
add_borough(dane)
return dane.Borough[(dane['Floor'] == 13)].unique()
2018-06-03 10:04:16 +02:00
def find_best_flats(dane):
2018-06-21 19:28:33 +02:00
add_borough(dane)
return dane[(dane['Borough'] == 'Winogrady') & (dane['Rooms'] == 3) & (dane['Floor'] == 1)]
2018-06-03 10:04:16 +02:00
def main():
dane = wczytaj_dane()
print(dane[:5])
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}"
.format(most_common_room_number(dane)))
2018-06-21 19:28:33 +02:00
print("{} to najładniejsza dzielnica w Poznaniu."
.format(find_borough("Grunwald i Jeżyce")))
2018-06-03 10:04:16 +02:00
print("Średnia cena mieszkania 3-pokojowego, to: {}"
.format(mean_price(dane, 3)))
2018-06-22 15:59:02 +02:00
2018-06-03 10:04:16 +02:00
if __name__ == "__main__":
2018-06-21 19:28:33 +02:00
main()