Pandas逐步过滤DataFrame:未定义过滤值的替代方案咨询
Pandas 扑克动作通用过滤方案
问题背景
我正在用Pandas搭建简易过滤系统,处理包含扑克手牌和玩家动作(call、raise、fold等)的数据库,想要实现一个通用过滤器,按玩家动作逐步过滤DataFrame:
- 过滤器包含action1、action2、action3、action4四个动作项
- 先将action1的过滤值设为"raise"(示例代码中用"c"作为测试值)
- action2、action3、action4暂未确定过滤条件
- 仅过滤action1,保留action2、action3、action4的所有可能值
但将action2、action3、action4设为空字符串时,过滤完全失效,请问应该用什么值替代?
附代码示例
import pandas as pd import numpy as np df = pd.read_csv('xxxxx', sep=";") df.dropna(inplace = True) ###PREFLOP### a1_preflop = "c" a2_preflop = "" a3_preflop = "" a4_preflop = "" x= df[(df.actions_1_preflop == a1_preflop) & (df.actions_2_preflop == a2_preflop) & (df.actions_3_preflop == a3_preflop)&(df.actions_4_preflop == a4_preflop)] print(x)
未过滤的小型DataFrame示例(目标:过滤action1_preflop="c",暂不过滤action2/3/4)
myposition tiers besthand_flop checker_flop handtype_flop topsuite_flop topcolor_flop besthand_turn checker_turn handtype_turn topsuite_turn topcolor_turn besthand_river checker_river handtype_river topsuite_river topcolor_river bet_1_preflop bet_2_preflop bet_3_preflop bet_1_flop bet_2_flop bet_3_flop bet_1_turn bet_2_turn bet_3_turn bet_1_river bet_2_river bet_3_river action1_preflop action2_preflop action3_preflop action4_preflop action1_flop action2_flop action3_flop action4_flop action1_turn action2_turn action3_turn action4_turn action1_river action2_river action3_river action4_river bb 4 Brelan 1 high 3 3 Brelan 1 high 3 3 Brelan 1 high 3 3 1.3 4.6 193.1 r c c e c a c c c sb 9 Double paire 0.5 very high 2 2 Double paire 0.5 very high 2 3 Double paire 0.5 very high 3 3 6 14 c e c c c c z f bb 9 Double paire 0.5 very high 2 3 Double paire 0.5 very high 3 3 Double paire 0.5 very high 4 3 2 9 188 c c c c r c c e a f sb 4 Paire 1 high 4 2 Paire 1 high 4 2 Suite 0.8 very high 5 2 2 1.5 5.85 189.65 c r c c c r c e a f sb 9 Paire 1 high 2 2 Double paire 0.5 high 2 2 Double paire 0.5 high 3 3 2 c c c c r c c c sb 9 Paire 1 high 3 2 Double paire 0.5 high 3 2 Brelan 0.666666667 high 3 3 3 2 1.2 c r c c c r c r c bb 9 Paire 1 high 2 3 Double paire 0.5 high 2 3 Brelan 1 high 2 3 1.3 c c c r c c c c c bb 9 Paire 1 high 3 2 Paire 1 high 3 2 Double paire 0.5 very high 3 2 1.3 197.7 r c c c a f sb 5 Paire 1 high 3 2 Paire 1 high 3 2 Double paire 1 high 3 2 1.3 197.7 c c c c r c c a f bb 7 Brelan 0.666666667 high 3 2 Brelan 0.666666667 high 3 3 Brelan 0.666666667 high 3 3 1.3 4.6 r c c c e f
解决方案
核心逻辑是:当某个动作项不需要过滤时,让对应的条件返回全True,这样在逻辑与(&)运算中不会影响其他条件的筛选结果。以下是三种可行方案:
方案1:用布尔值True作为占位符
将不需要过滤的变量设为True,通过类型判断决定是否应用该条件:
import pandas as pd import numpy as np df = pd.read_csv('xxxxx', sep=";") df.dropna(inplace=True) ###PREFLOP### a1_preflop = "c" a2_preflop = True # 不需要过滤时设为True a3_preflop = True a4_preflop = True # 构建过滤条件 cond1 = df.actions_1_preflop == a1_preflop cond2 = df.actions_2_preflop == a2_preflop if not isinstance(a2_preflop, bool) else True cond3 = df.actions_3_preflop == a3_preflop if not isinstance(a3_preflop, bool) else True cond4 = df.actions_4_preflop == a4_preflop if not isinstance(a4_preflop, bool) else True x = df[cond1 & cond2 & cond3 & cond4] print(x)
方案2:动态构建过滤条件列表
只添加需要过滤的条件,避免冗余判断,扩展性最强:
import pandas as pd import numpy as np df = pd.read_csv('xxxxx', sep=";") df.dropna(inplace=True) ###PREFLOP### filters = [] # 添加需要过滤的条件 filters.append(df.actions_1_preflop == "c") # 后续需要过滤action2时,直接追加条件即可 # filters.append(df.actions_2_preflop == "raise") # filters.append(df.actions_3_preflop == "fold") # 合并所有条件 x = df[np.all(filters, axis=0)] print(x)
方案3:用None作为占位符
将不需要过滤的变量设为None,通过判断变量是否为None来决定是否应用条件:
import pandas as pd import numpy as np df = pd.read_csv('xxxxx', sep=";") df.dropna(inplace=True) ###PREFLOP### a1_preflop = "c" a2_preflop = None a3_preflop = None a4_preflop = None cond1 = df.actions_1_preflop == a1_preflop cond2 = df.actions_2_preflop == a2_preflop if a2_preflop is not None else True cond3 = df.actions_3_preflop == a3_preflop if a3_preflop is not None else True cond4 = df.actions_4_preflop == a4_preflop if a4_preflop is not None else True x = df[cond1 & cond2 & cond3 & cond4] print(x)
以上三种方案都能实现仅过滤指定动作项、保留其他动作所有可能值的需求,其中方案2的灵活性最高,后续添加新过滤条件只需追加列表项即可。
内容的提问来源于Stack Exchange,提问作者Raphaël Ambit
相关产品推荐
相关产品推荐

