1. import pandas as pd # データ分析に用いるライブラリ
  2. import matplotlib.pyplot as plt # グラフ表示に用いるライブラリ
  3. pd.set_option('display.unicode.east_asian_width', True) # 表示のずれを少し緩和
  4. plt.rcParams['font.family'] = 'IPAexGothic' # グラフ表示におけるフォントの指定
  5. data_path="./data.csv"
  6. df_data = pd.read_csv(data_path, encoding="utf-8-sig")
  7. # print(df_data["合計時間"].describe())
  8. # print(df_data.groupby(["間取り"]).count())
  9. print("物件数", len(df_data), "件")
  10. # 全体の相場を調べる---------------------------------------
  11. roomtype=input("間取りを入力してください")
  12. mask = (df_data["間取り"] == roomtype)
  13. df_selected_roomtype=df_data[mask]
  14. print("物件数",len(df_selected_roomtype))
  15. # pd.set_option('display.max_columns', None)
  16. # # 駅、路線、家賃、名称、合計時間、URLを表示する
  17. # print(df_selected_roomtype[["駅", "路線","家賃", "名称", "合計時間", "URL"]].sort_values("家賃").to_string(index=False))
  18. df_selected_roomtype.loc[:, "家賃"].hist(range=(0,50),bins=40)
  19. plt.xlabel(roomtype+"家賃(万円)") # 横軸のラベル
  20. plt.ylabel("件数") # 縦軸のラベル
  21. plt.xlim(0, 50)
  22. plt.title(roomtype+"家賃のヒストグラム") # グラフのタイトル
  23. plt.show()
  24. # -------------------------------------------------------------
  25. # 路線毎の相場 -------------------------------------
  26. axes = df_selected_roomtype.loc[:, "家賃"].hist(by=df_selected_roomtype.loc[:, "路線"],
  27.                                                  range=(0, 50), bins=30,
  28.                                                  figsize=(14, 7),
  29.                                                  sharex=True, sharey=True)
  30. for ax in axes.reshape(-1):
  31.     ax.grid(b=True) # グリッドを表示
  32.     ax.set_xlabel("家賃(万円)") # 横軸のラベル
  33.     ax.set_ylabel("件数") # 縦軸のラベル
  34.     # ax.set_ylim(0, 120) # 縦軸の目盛りの最小値と最大値の指定
  35. plt.suptitle("路線ごとの"+roomtype+"家賃のヒストグラム") # グラフ全体のタイトル
  36. plt.subplots_adjust(top=0.92, # グラフ位置の微調整
  37.                     hspace=0.3) # グラフ間の微調整
  38. plt.show()
  39. # ------------------------------------------------------
  40. # 希望家賃(万)
  41. rent_upper = int(input("希望家賃の上限は?(万円)"))
  42. rent_lower = int(input("希望家賃の下限は?(万円)"))
  43. # 家賃で絞り込む
  44. mask = (df_selected_roomtype['家賃'] <= rent_upper) & (df_selected_roomtype['家賃'] >= rent_lower)
  45. # 絞った内容をfilter_dfに入れる
  46. filter_df = df_selected_roomtype[mask]
  47. print("家賃で絞り込んだ結果", len(filter_df), "件")
  48. # 通勤・通学時間---------------------------------------------------
  49. filter_df.loc[:, "合計時間"].hist(range=(0,120),bins=120)
  50. plt.xlabel("通勤・通学時間(分)") # 横軸のラベル
  51. plt.ylabel("件数") # 縦軸のラベル
  52. plt.xlim(0, 120)
  53. plt.title(str(rent_lower) + "-" + str(rent_upper)+"万円の"+roomtype+":通勤・通学時間ヒストグラム") # グラフのタイトル
  54. plt.show()
  55. #----------------------------------------------------------
  56. # 通勤時間(分)
  57. commuting_time = float(input("希望通勤時間上限は?(分)"))
  58. mask = (filter_df['合計時間'] <= commuting_time)
  59. # filter_dfをさらに合計時間で絞り込む(filter_dfを上書き)
  60. filter_df = filter_df[mask]
  61. print("家賃,通勤・通学時間で絞り込んだ結果", len(filter_df), "件")
  62. # バスは使いますか?
  63. bus = input('バスは使ってもいいですか? y or n')
  64. if bus == 'n':
  65.     # バスなしであれば、バスは0
  66.     bus_num = 0
  67. else:
  68.     # とりあえずありえない数字を入れておく
  69.     bus_num = 10000
  70. mask=(filter_df['バス'] <= bus_num)
  71. # filter_dfをさらにバス有無で絞り込み
  72. filter_df = filter_df[mask]
  73. print("家賃,通勤・通学時間、バス有無で絞り込んだ結果", len(filter_df), "件")
  74. # 条件にあう分件が多い駅top10を表示(書き方難しかった)
  75. filter_top10_df = filter_df.groupby(['駅'])['駅'].count().sort_values(ascending=False).head(10)
  76. # 続けるかどうかのフラグ
  77. flag = True
  78. while (flag):
  79.     print('あなたにおすすめの駅は')
  80.     print(filter_top10_df)
  81.     #  条件に合致する物件数が多い上位10の駅を「おすすめ駅」とする
  82.     # 絞り込んだ結果をさらに「おすすめ駅」で絞り込み(ここちょっと書き方難しい)
  83.     mask = filter_df['駅'].isin(filter_top10_df.index)
  84.     # filter_dfからおすすめ駅に絞り込んだ結果をfilter_top10_station_dfに入れる
  85.     filter_top10_station_df = filter_df[mask]
  86.     # filter_top10_station_df.loc[:, "家賃"].hist(by=filter_top10_station_df.loc[:, "駅"],range=(rent_lower,rent_upper),bins=10,figsize=(8, 8))
  87.     # plt.show()
  88.     # ヒストグラム-------------------------------------
  89.     axes = filter_top10_station_df.loc[:, "家賃"].hist(by=filter_top10_station_df.loc[:, "駅"],
  90.                                                      range=(rent_lower, rent_upper), bins=10,
  91.                                                      figsize=(10, 8),
  92.                                                      sharex=True, sharey=True)
  93.     for ax in axes.reshape(-1):
  94.         ax.grid(b=True) # グリッドを表示
  95.         ax.set_xlabel("家賃(万円)") # 横軸のラベル
  96.         ax.set_ylabel("件数") # 縦軸のラベル
  97.     plt.suptitle("おすすめ駅:家賃のヒストグラム(家賃制限内)") # グラフ全体のタイトル
  98.     plt.subplots_adjust(top=0.92, # グラフ位置の微調整
  99.                         hspace=0.3) # グラフ間の微調整
  100.     plt.show()
  101.     # ------------------------------------------------------
  102.     print("おすすめ駅(10駅)物件数", len(filter_top10_station_df))
  103.     station = input('駅名を入力してください')
  104.     # おすすめ駅Top10(filter_top10_station_df)から駅名で更に絞り込みfilter_selected_station_dfに入れる
  105.     mask = (filter_top10_station_df['駅'] == station)
  106.     filter_selected_station_df = filter_top10_station_df[mask]
  107.     print(station + "の合致物件数", len(filter_selected_station_df), "件")
  108.     # 全件表示させるようにする
  109.     pd.set_option('display.max_rows', None)
  110.     pd.set_option('display.max_columns', None)
  111.     # 選択した駅の駅、路線、家賃、名称、合計時間、URLを表示する
  112.     print(filter_selected_station_df[["駅", "路線","家賃", "名称", "合計時間", "URL"]].sort_values("家賃").to_string(index=False))
  113.     print("----------------------------------------------------------------")
  114.     loop_input = input("続けますか? y or n")
  115.     if loop_input == 'n':
  116.         flag = False