簡體   English   中英

使用 python 循環遍歷列表並插入到 str.contains 中(並計算存在多個項目的 df 行)

[英]looping through a list and inserting into str.contains (and counting rows of a df where multiple items are present) using python

我的目標是設計一個 function 將需要兩個 arguments - 一個是玩撲克的人的列表,下一個是可能的動作列表(例如跟注,加注) - 並在列上使用 str.contains 來了解每個玩家的頻率做每一個動作。

DataFrame df有幾列,但我想將 function 應用於標題為“條目”的列,該列包含在線撲克桌上發生的所有操作的日志(列中的每一行都是一個字符串)。

這就是“條目”列的樣子(每一行都是一個字符串):

-- 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)])返回正確的值(我使用正則表達式,因為我是這兩個詞尋找在同一行,中間有一堆不同的字符)。

該問題似乎與嘗試將值循環到str.contains的字符串模式有關。 如何遍歷列表並打印姓名以及該人執行給定輸入操作的次數?

理想情況下,我想同時遍歷代碼頂部的兩個列表。

這行得通嗎?

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)

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM