我有一个具有两个维度的熊猫数据框:'col1' 和 'col2'
我可以使用以下方法过滤这两列的某些值:
df[ (df["col1"]=='foo') & (df["col2"]=='bar')]
Run Code Online (Sandbox Code Playgroud)
有什么办法可以一次过滤两列吗?
我天真地尝试将数据框限制为两列,但我对等式第二部分的最佳猜测不起作用:
df[df[["col1","col2"]]==['foo','bar']]
Run Code Online (Sandbox Code Playgroud)
给我这个错误
ValueError: Invalid broadcasting comparison [['foo', 'bar']] with block values
Run Code Online (Sandbox Code Playgroud)
我需要这样做是因为列的名称以及设置条件的列数会有所不同