如何使用pandas输入功能获取客户列表

2024-03-29 08:28:09 发布

您现在位置:Python中文网/ 问答频道 /正文

我已经创建了一个代码,让我的平台的用户基于两件事:

  • choiceTitle:搜索我的平台用户看过的广告标题中包含的特定单词。例如,广告是“我们提供免费杜松子酒”,我想得到“杜松子酒”这个词
  • PrimaryTagPreviousChoice:广告上有一个“食品和饮料”标签

我可以让那些对杜松子酒、食物和饮料感兴趣的用户使用:

(df2['choiceTitle'].str.contains(“(?i)Gin”)&;(df2['PrimaryTagPreviousChoice'].str.包含((?i)食品和饮料)

我想做的是创建一个包含所有代码的函数(因此包括sql查询、重命名操作、排序值)​​操作等),然后使用输入功能。所以我只需要运行我的代码,这样python就会问我两个问题:

  • 选择题。。。杜松子酒
  • PrimaryTagPreviousChoice。。。食物和饮料

我输入这两个选项,它会给我提供对杜松子酒、食物和饮料感兴趣的用户

我怎么做

我的代码:

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) 

2条回答

在Python中添加函数很简单。只需使用def关键字来声明函数,并将现有代码放在它下面(缩进)。将参数放在括号中

以下是更新的代码:

def GetUsers (title, tag)
    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)" + title)) &   
        (df2['PrimaryTagPreviousChoice'].str.contains("(?i)" + tag))]

    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) 
    return dw  # send back to print statement


# get input from user
inpTitle = input ("choiceTitle? ")
inpTag = input ("PrimaryTagPreviousChoice? ")

# run function
result = GetUsers (inpTitle, inpTag)

print(result)

试试这个。将input()保存为变量,并使用字符串连接编辑掩码。注意,转义需要一组额外的{}

choiceTitle = input('choiceTitle?')
PrimaryTagPreviousChoice = input('PrimaryTagPreviousChoice?')

mask = df2[(df2['choiceTitle'].str.contains("(?i){{0}}".format(choiceTitle))) & 
           (df2['PrimaryTagPreviousChoice'].str.contains("(?i) 
           {{0}}".format(PrimaryTagPreviousChoice)))]

dw = mask[['user', 'title', 'user_category', 'email', 'last_login', 
          'PrimaryTagPreviousChoice', 'choiceTitle'
         ]].drop_duplicates()

....

相关问题 更多 >