#!/usr/bin/env python # -*- coding: utf-8 -*- import pandas as pd import matplotlib.pyplot as plt def wczytaj_dane ( ): return pd.read_csv ( 'mieszkania.csv' ) def most_common_room_number ( dane ): return dane[ 'Rooms' ].value_counts ( ) def cheapest_flats ( dane , n ): cheapest = dane[ [ 'Expected' , 'Location' ] ].sort_values ( by=[ "Expected" ] , ascending=False ) return cheapest[ :n ] def find_borough ( desc ): dzielnice = [ 'Stare Miasto' , 'Wilda' , 'Jeżyce' , 'Rataje' , 'Piątkowo' , 'Winogrady' , 'Miłostowo' , 'Dębiec' ] descSplit = desc.split ( ' ' ) dzielnica = [ ds for ds in descSplit if ds in dzielnice ] return dzielnica[ 0 ] if len ( dzielnica ) > 0 else 'Inne' def add_borough ( dane ): dane[ 'Borough' ] = dane.apply ( lambda row: find_borough ( row[ 'Location' ] ) , axis=1 ) return dane def write_plot ( dane , filename ): dane2 = add_borough ( dane ) do_plota = dane2[ 'Borough' ].value_counts ( ) do_plota.plot ( kind='bar' ) # plt.show() plt.savefig ( filename ) def mean_price ( dane , room_number ): pokoje = dane[ dane[ 'Rooms' ] == room_number ] return pokoje[ 'Expected' ].mean ( ) def find_13 ( dane ): dane2 = add_borough ( dane ) pietro = dane2[ dane2[ 'Floor' ] == 13 ] return ' '.join ( set ( pietro[ 'Borough' ].tolist ( ) ) ) def find_best_flats ( dane ): dane2 = add_borough ( dane ) isWinogrady = dane2[ 'Borough' ] == 'Winogrady' isPierwsze = dane2[ 'Floor' ] == 1 isTrzypokojowe = dane2[ 'Rooms' ] == 3 best = dane2[ isWinogrady & isPierwsze & isTrzypokojowe ] return best 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.".format ( find_borough ( "Grunwald i Jeżyce" ) )) print("Średnia cena mieszkania 3-pokojowego, to: {:.2f}".format ( mean_price ( dane , 3 ) )) print("Dzielnice z ofertami na 13 pietrze: {}".format ( find_13 ( dane ) )) print("Najlepsze oferty: {}".format ( find_best_flats ( dane ) )) if __name__ == "__main__": main ( )