1
0
forked from tdwojak/Python2017
Python2017/labs06/task02.py

72 lines
2.0 KiB
Python
Raw Permalink Normal View History

2017-12-15 14:24:17 +01:00
#!/usr/bin/env python
# -*- coding: utf-8 -*-
2017-12-16 13:29:56 +01:00
import pandas as pd
2018-01-06 00:11:57 +01:00
import sys
import numpy as np
2017-12-15 14:24:17 +01:00
def wczytaj_dane():
2017-12-16 13:29:56 +01:00
my_data = pd.read_csv('mieszkania.csv',
encoding='utf-8',
2018-01-06 00:11:57 +01:00
index_col='Id',
sep = ',')
2017-12-16 13:29:56 +01:00
return my_data
#print(my_data)
2017-12-15 14:24:17 +01:00
def most_common_room_number(dane):
2018-01-06 00:11:57 +01:00
rooms = dane['Rooms']
rooms_max = rooms.value_counts().index[0]
return rooms_max
2017-12-16 13:29:56 +01:00
#return ( room_num_list[room_num_list.index(max(room_num_list))] )
#pass
2017-12-15 14:24:17 +01:00
def cheapest_flats(dane, n):
2018-01-06 00:11:57 +01:00
expected = dane['Expected']
n_cheapest = expected.sort_values().head(n)
return n_cheapest
2017-12-15 14:24:17 +01:00
def find_borough(desc):
dzielnice = ['Stare Miasto',
'Wilda',
'Jeżyce',
'Rataje',
'Piątkowo',
'Winogrady',
'Miłostowo',
'Dębiec']
2018-01-06 00:11:57 +01:00
for dzielnica in dzielnice:
if dzielnica in desc:
return dzielnica
return 'Inne'
2017-12-15 14:24:17 +01:00
def add_borough(dane):
2018-01-06 00:11:57 +01:00
dane['Borough'] = dane.apply(lambda row: find_borough(row['Location']))
2017-12-15 14:24:17 +01:00
def write_plot(dane, filename):
2018-01-06 00:11:57 +01:00
dane['Borough'].value_counts().plot.bar().get_figure().savefig(filename)
2017-12-15 14:24:17 +01:00
def mean_price(dane, room_number):
2018-01-06 00:11:57 +01:00
mean_value = dane.loc[dane['Rooms'] == room_number]['Expected'].mean()
return mean_value
2017-12-15 14:24:17 +01:00
def find_13(dane):
2018-01-06 00:11:57 +01:00
return dane.loc[dane['Floor'] == 13]['Borough'].unique()
2017-12-15 14:24:17 +01:00
def find_best_flats(dane):
2018-01-06 00:11:57 +01:00
best_flats = dane.loc[(df['Borough'] == 'Winogrady') & (dane['Rooms'] == 3) & (dane['Floor'] == 1)]
return best_flats
2017-12-15 14:24:17 +01:00
def main():
dane = wczytaj_dane()
print(dane[:5])
print("Najpopularniejsza liczba pokoi w mieszkaniu to: {}"
.format(most_common_room_number(dane)))
print("{} to najłądniejsza dzielnica w Poznaniu."
2017-12-16 13:29:56 +01:00
.format(find_borough("Grunwald i Jeżyce")))
2017-12-15 14:24:17 +01:00
print("Średnia cena mieszkania 3-pokojowego, to: {}"
.format(mean_price(dane, 3)))
if __name__ == "__main__":
main()