sharing

import pandas as pd
import numpy as np
import randomdf=pd.DataFrame(data=data)# print(df)
#描述性分析:求min、max、count,nunique
# print(df["name"].count())
# print(df["search_index"].max())
# print(df["search_index"].min())
# print(df["type"].nunique())
# print(np.unique(df["type"]))
#分组统计
#聚合统计 groupby
# #1. 字典方案
# #根据电视剧类型,统计搜索指数的最大值,搜索指数的总量,电视剧数量。
# tb1=df.groupby(by="type").agg(
#     {"search_index":[np.max,"sum"],
#      "trending":["count"]
#     })
# print(tb1)
# # #列表式
# # #在搜索趋势上升的电视剧中,统计搜索指数的最小值,搜索趋势上升的电视剧数量。
# tb2=df.loc[df["trending"]=="increase"].groupby(by="type")["search_index"].agg(["min","count"])
# print(tb2)#命名式#根据趋势情况(上升,下降,不变)及影片类型统计电视剧数量
tb3=df.groupby(by=["type","trending"]).agg(电视剧数量=('name','count'))
# print(tb3)
# # # #改变索引
#print(type(tb3))
tb3.reset_index(drop=False,inplace=True)
# print(tb3)
# #改变列名
tb3.columns=['电视剧类型','搜索趋势','电视剧数量']
# print(tb3)
#restart_game=1
# #
# # #1.指定游戏难度
def difficulty():list=[]i=0your_number=int(input("choose a difficulty.Type 1 to 5:"))while i <=(your_number+5):append_data=random.choice(data)if append_data not in list:list.append(append_data)i+=1return list#判断是否闯关成功
def success(score):if score==3:result="成功"else:result="失败"return result
#2.闯关(完成3道题目)
while restart_game==1:score=0subject=difficulty()Q=pd.DataFrame(data=subject)print(Q.loc[:,"name"])question1=str(input("请问,您认为以上电视剧中搜索指数最高的是哪部电视剧:"))
#     #正确的答案通过max函数获取最大值,并通过loc的key来访问到最大值对应的电视剧名称max_value=Q.loc[Q["search_index"]==Q["search_index"].max(),"name"].values[0]if question1==max_value:print("您答对了第一题,请继续!")score+=1else:print("您打错了,抱歉")question2=int(input("请问,您认为以上电视剧中本周搜索趋势上升的电视剧有几部:"))increase_value = Q.loc[Q["trending"] == "increase"].agg({"name": ["count"]}).values[0][0]if question2 == increase_value:print("您答对了第二题,请继续!")score += 1else:print("您打错了,抱歉")#本次随机筛选电视剧的所有类型type_catelog=np.unique(Q["type"])type_1=random.choice(type_catelog)type_2=random.choice(type_catelog)if type_2==type_1:type_2 = random.choice(type_catelog)#字符串格式化question3 = str(input(f"请问,您认为以上电视剧中{type_1}类电视剧中和{type_2}类电视剧谁更受欢迎(表现为搜索指数总和更高):"))
#     #设置条件,只需要对需要比较的电视剧类型进行求和agg_result = Q.loc[(Q["type"] == type_1) | (Q["type"] == type_2)].groupby(by="type").agg({"search_index": ["sum"]})# 重索引agg_result.reset_index(drop=False, inplace=True)# 改变列名agg_result.columns = ['type', 'total']agg_result_T = agg_result.Ttype_1_number = agg_result_T.iloc[1, 0]type_2_number = agg_result_T.iloc[1, 1]answer_number=[type_1_number if type_1_number>=type_2_number else type_2_number][0]answer=agg_result.loc[agg_result["total"]==answer_number,"type"].values[0]if question3 == answer:print("您答对了第三题,闯关成功!")score += 1else:print("您打错了,抱歉")result=success(score)print(f"您本次得分:{score},结果为:闯关{result}")restart_game=int(input("想要重新开始吗?按1重新开始,按其他键退出:"))#
#
data=[{"name":"黑暗荣耀第二季","search_index":388113,"type":"悬疑","actors":"宋慧乔、李到晛","trending":"increase"},{"name": "星落凝成糖","search_index": 269476,"type": "爱情","actors": "陈星旭、李兰迪","trending":"decrease"},{"name": "归路","search_index": 703518,"type": "都市","actors": "井柏然、谭松韵","trending":"unchanged"},{"name": "他是谁","search_index": 1435500,"type": "悬疑","actors": "张译、赵阳","trending":"increase"},{"name": "风雨送春归","search_index": 63327,"type": "都市","actors": "王志文、于震","trending":"decrease"},{"name": "情满九道弯","search_index": 55807,"type": "都市","actors": "韩东君、热依扎","trending":"increase"},{"name": "重紫","search_index": 54646,"type": "古装","actors": "杨超越、徐正溪",
"trending":"unchanged"},{"name": "回廊亭","search_index": 50673,"type": "犯罪","actors": "邓家佳、张新成",
"trending":"decrease"},{"name": "去有风的地方","search_index": 45310,"type": "都市","actors": "刘亦菲、李现",
"trending":"decrease"},{"name": "我们的日子","search_index": 43039,"type": "都市","actors": "李小冉、李乃文",
"trending":"increase"},{"name": "风吹半夏","search_index": 26553,"type": "爱情","actors": "赵丽颖、欧豪",
"trending":"decrease"},{"name": "心居","search_index": 22174,"type": "都市","actors": "海清、童瑶 ",
"trending":"decrease"},{"name": "县委大院","search_index": 11158,"type": "都市","actors": "胡歌、吴越",
"trending":"unchanged"},{"name": "对我而言危险的他","search_index": 24055,"type": "悬疑","actors": "李墨之、樊治欣",
"trending":"increase"},{"name": "勇者无惧","search_index": 12257,"type": "犯罪","actors": "张丹峰、袁雨萱",
"trending":"increase"},{"name": "梦华录","search_index": 14375,"type": "古装","actors": "刘亦菲、陈晓",
"trending":"decrease"},{"name": "狂飙","search_index": 19420,"type": "都市","actors": "张译、张颂文",
"trending":"unchanged"},{"name": "唐朝诡事录","search_index": 14653,"type": "悬疑","actors": "杨志刚、杨旭文",
"trending":"increase"}]


本文来自互联网用户投稿,文章观点仅代表作者本人,不代表本站立场,不承担相关法律责任。如若转载,请注明出处。 如若内容造成侵权/违法违规/事实不符,请点击【内容举报】进行投诉反馈!

相关文章

立即
投稿

微信公众账号

微信扫一扫加关注

返回
顶部