我已经创建了一个代码,让我的平台的用户基于两件事:
我可以让那些对杜松子酒、食物和饮料感兴趣的用户使用:
(df2['choiceTitle'].str.contains(“(?i)Gin”)&;(df2['PrimaryTagPreviousChoice'].str.包含((?i)食品和饮料)
我想做的是创建一个包含所有代码的函数(因此包括sql查询、重命名操作、排序值)操作等),然后使用输入功能。所以我只需要运行我的代码,这样python就会问我两个问题:
我输入这两个选项,它会给我提供对杜松子酒、食物和饮料感兴趣的用户
我怎么做
我的代码:
df = pd.read_sql_query(""" select etc..... """, con)
df1 = pd.read_sql_query(""" select etc..... """, con)
df1['user_id'] = df1['user_id'].apply(str)
df2 = pd.merge(df, df1, left_on='user_id', right_on='user_id', how='left')
tag = df2[
(df2['choiceTitle'].str.contains("(?i)Gin")) &
(df2['PrimaryTagPreviousChoice'].str.contains("(?i)Food and Drink"))
]
dw = tag[['user', 'title', 'user_category', 'email', 'last_login',
'PrimaryTagPreviousChoice', 'choiceTitle'
]].drop_duplicates()
dw = dw.sort_values(['last_login'], ascending=[False])
dw = dw[dw.last_login > dt.datetime.now() - pd.to_timedelta("30day")]
dw = dw.rename({'user': 'user full name', 'title': 'user title'}
, axis='columns')
dw.drop_duplicates(subset ="Email",
keep = 'first', inplace = True)
在Python中添加函数很简单。只需使用def关键字来声明函数,并将现有代码放在它下面(缩进)。将参数放在括号中
以下是更新的代码:
试试这个。将
input()
保存为变量,并使用字符串连接编辑掩码。注意,转义需要一组额外的{}
相关问题 更多 >
编程相关推荐