In [39]:
%pylab inline
import pandas as pd
import os
from IPython.display import display
from IPython.html.widgets import interact_manual, interact
from random import choice
'''
店名,距離,價位
stores_lst = [['牛肉麵','中','中'],
['燉肉飯','中','低'],
['壽司', '中', '高'],
stores_df = pd.DataFrame(stores_lst, columns=["店名","距離","價位"])
'''
areas = [file for file in os.listdir() if file[-4:] == '.csv']
try:
stores_df = pd.read_csv(areas[0], encoding ='utf8')
except UnicodeDecodeError:
stores_df = pd.read_csv(areas[0], encoding ='big5')
def 選擇菜單(area):
global stores_df
print (area)
try:
stores_df = pd.read_csv(area, encoding ='utf8')
except UnicodeDecodeError:
stores_df = pd.read_csv(area, encoding ='big5')
def 上帝選吧():
print (choice(stores_df["店名"]))
def 三選一吧():
tmp_df = stores_df
c1 = choice(tmp_df["店名"])
print (c1)
tmp_df = tmp_df[tmp_df["店名"] != c1]
tmp_df.index = range(0,len(tmp_df))
#print (tmp_df)
c2 = choice(tmp_df["店名"])
print (c2)
tmp_df = tmp_df[tmp_df["店名"] != c2]
tmp_df.index = range(0,len(tmp_df))
#print (tmp_df)
c3 = choice(tmp_df["店名"])
print (c3)
#tmp_df = tmp_df[tmp_df["店名"] != c3]
#print (tmp_df)
def choose_by_distance(distance):
'''
distance: 遠, 中, 近
'''
gp = stores_df.groupby("距離")
new_df = gp.get_group(name=distance)
new_df.index = range(0,len(new_df))
print (choice(new_df["店名"]))
def 近一點():
choose_by_distance('近')
def 不近不遠():
choose_by_distance('中')
def 遠一點():
choose_by_distance('遠')
def choose_by_price(price):
'''
price: 高, 中, 低
'''
gp = stores_df.groupby("價位")
new_df = gp.get_group(name=price)
new_df.index = range(0,len(new_df))
print (choice(new_df["店名"]))
def 便宜一點():
choose_by_price('低')
def 價位適中():
choose_by_price('中')
def 貴一點():
choose_by_price('高')
def 給我選():
display(stores_df)
interact(選擇菜單, area=areas);
interact_manual(上帝選吧);
interact_manual(三選一吧);
interact_manual(近一點);
interact_manual(不近不遠);
interact_manual(遠一點);
interact_manual(便宜一點);
interact_manual(價位適中);
interact_manual(貴一點);
interact_manual(給我選);
In [3]:
help(choice)
In [35]:
In [30]:
help(pd.core.frame.DataFrame.filter)
In [ ]: