使用python在列表中循环并插入str.contains(并计算存在多个项的df的行数)

2024-09-26 22:09:48 发布

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

我的目标是设计一个包含两个参数的函数——一个是玩扑克的人的列表,另一个是可能的动作的列表(例如,调用、提升)——并在列上使用str.contains找出每个玩家执行每个动作的频率

DataFramedf有几个列,但我只想将该函数应用于标题为“entry”的列,该列包含在线扑克表中发生的所有操作的日志(列中的每一行都是字符串)

这是“entry”列的外观(每行是一个字符串):

-- ending hand #174 --
"Prof @ ZY_G_5ZOve" gained 100
"tom_thumb @ g1PBaozt7k" folds
"Prof @ ZY_G_5ZOve" calls with 50
"tom_thumb @ g1PBaozt7k" checks
river: 9♦, 5♣, Q♥, 7♠ [K♠]
"Prof @ ZY_G_5ZOve" checks
"tom_thumb @ g1PBaozt7k" checks
turn: 9♦, 5♣, Q♥ [7♠]
"Prof @ ZY_G_5ZOve" checks
"tom_thumb @ g1PBaozt7k" checks
flop:  [9♦, 5♣, Q♥]
"Prof @ ZY_G_5ZOve" checks
"tom_thumb @ g1PBaozt7k" calls with 50
"Bob T. @ fjZTXUGV2G" folds
"danny G @ tNE1_lEFYv" folds
"Prof @ ZY_G_5ZOve" posts a big blind of 50
"tom_thumb @ g1PBaozt7k" posts a small blind of 25
-- starting hand #174  (Texas Hold'em) (dealer: "Bob T. @ fjZTXUGV2G") --
-- ending hand #173 --
"tom_thumb @ g1PBaozt7k" gained 475
"danny G @ tNE1_lEFYv" folds
"Prof @ ZY_G_5ZOve" folds
"tom_thumb @ g1PBaozt7k" raises with 356
flop:  [4♥, A♠, 6♠]
"danny G @ tNE1_lEFYv" calls with 150
"Prof @ ZY_G_5ZOve" calls with 150
"tom_thumb @ g1PBaozt7k" raises with 150
"Bob T. @ fjZTXUGV2G" folds
"danny G @ tNE1_lEFYv" calls with 50
"Prof @ ZY_G_5ZOve" calls with 50
"tom_thumb @ g1PBaozt7k" posts a big blind of 50
"Bob T. @ fjZTXUGV2G" posts a small blind of 25
-- starting hand #173  (Texas Hold'em) (dealer: "danny G @ tNE1_lEFYv") --

以下是我尝试过的一些示例代码:

player_list = ['danny G', 'Jane', 'Prof', 'spn', 'tim', 'Bob T.', 'joon', 'tom_thumb']
action_list = ['call', 'fold']

def action_amount(df, player_list, action):
    for player in player_list:
        action_number =len(df[df['entry'].str.contains('(player).*(action)', regex=True)])
        print(f'{player} {action}ed {action_number} times.')

action_amount(df, player_list, 'call')

现在,格式正确,但我无法将列表中的项目循环到str.contains,因此结果如下:

danny G called 0 times.
Jane called 0 times.
Prof called 0 times.
spn called 0 times.
tim called 0 times.
Bob T. called 0 times.
joon called 0 times.
tom_thumb called 0 times.

对于上面的样本df['entry']信息,它应该返回:

danny G called 2 times.
Jane called 0 times.
Prof called 3 times.
spn called 0 times.
tim called 0 times.
Bob T. called 0 times.
joon called 0 times.
tom_thumb called 1 times.

值得注意的是,len(df[df['entry'].str.contains('(danny G).*(call)', regex=True)])返回正确的值(我使用regex是因为我要查找的两个单词在同一行中,中间有一组不同的字符)

这个问题似乎与试图将值循环到str.contains的字符串模式有关。如何循环浏览列表并打印姓名以及执行给定输入操作的次数

理想情况下,我希望同时遍历代码顶部的两个列表


Tags: dfwithactionbobplayertimestomprof
1条回答
网友
1楼 · 发布于 2024-09-26 22:09:48

这样行吗

def action_amount(df, player_list, action_list):
    for player in player_list:
        for action in action_list:
            pattern = f'{player}.*{action}'
            matching_rows = df[df['entry'].str.contains(pattern, regex=True)]
            action_number = len(matching_rows)
            print(f'{player} {action}ed {action_number} times.')

action_amount(df, player_list, possible_actions)

相关问题 更多 >

    热门问题