1
0
forked from tdwojak/Python2017
Python2017/labs06/task02.py
2017-12-25 12:08:16 +01:00

86 lines
2.0 KiB
Python
Executable File

#!/usr/bin/env python
# -*- coding: utf-8 -*-
import pandas as pd
import matplotlib
import matplotlib.pyplot as plt
def wczytaj_dane():
dane = pd.read_csv('mieszkania.csv',
sep=',',
encoding='utf-8')
return dane
def most_common_room_number(dane):
return dane.Rooms.mode()[0]
def cheapest_flats(dane, n):
dane = dane.sort_values('Expected',ascending=True)
return dane.head(n)
def find_borough(desc):
dzielnice = ['Stare Miasto',
'Wilda',
'Jeżyce',
'Rataje',
'Piątkowo',
'Winogrady',
'Miłostowo',
'Dębiec']
for dzielnica in dzielnice:
if desc.find(dzielnica)>=0:
return dzielnica
return 'Inne'
def add_borough(dane):
borough = []
for current_location in dane:
borough.append(find_borough(current_location))
return pd.Series(borough)
def write_plot(dane, filename):
dane['Borough'].hist()
plt.savefig(filename)
def mean_price(dane, room_number):
dane = dane[dane.Rooms == room_number]
return round(dane.Expected.mean(),2)
def find_13(dane):
dane = dane[dane.Floor == 13]
return list(dane.Borough)
def find_best_flats(dane):
dane = dane[(dane.Borough=='Winogrady') & (dane.Rooms==3) & (dane.Floor == 1)]
return dane
def main():
dane = wczytaj_dane()
print(dane[:5])
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}"
.format(most_common_room_number(dane)))
najtansze = cheapest_flats(dane,10)
print("{} to najłądniejsza dzielnica w Poznaniu."
.format(find_borough("Grunwald i Jeżyce")))
dzielnice = add_borough(dane['Location'])
dane['Borough'] = dzielnice.values
write_plot(dane,'wykres.png')
print("Średnia cena mieszkania 3-pokojowego, to: {}"
.format(mean_price(dane, 3)))
find_13(dane)
find_best_flats(dane)
if __name__ == "__main__":
main()