首页 \ 问答 \ 使用任意数量的条件过滤Pandas Dataframe(Filter Pandas Dataframe using an arbitrary number of conditions)

使用任意数量的条件过滤Pandas Dataframe(Filter Pandas Dataframe using an arbitrary number of conditions)

我很熟悉使用Pandas进行基本过滤和查询。 例如,如果我有一个名为df的数据帧,我可以做df[df['field1'] < 2]df[df['field2'] < 3] 。 我也可以将多个标准链接在一起,例如:

df[(df['field1'] < 3) & (df['field2'] < 2)]

如果我事先不知道需要使用多少条件怎么办? 有没有办法将任意数量的这些操作“链接”在一起? 我想传递一个过滤器列表,如[('field1', 3), ('field2', 2), ('field3', 4)] ,这将导致这三个条件链接在一起。

谢谢!


I am comfortable with basic filtering and querying using Pandas. For example, if I have a dataframe called df I can do df[df['field1'] < 2] or df[df['field2'] < 3]. I can also chain multiple criteria together, for example:

df[(df['field1'] < 3) & (df['field2'] < 2)].

What if I don't know in advance how many criteria I will need to use? Is there a way to "chain" an arbitrary number of these operations together? I would like to pass a list of filters such as [('field1', 3), ('field2', 2), ('field3', 4)] which would result in the chaining of these 3 conditions together.

Thanks!


原文:https://stackoverflow.com/questions/47147414
更新时间:2024-01-19 07:01

最满意答案

你需要通过Javascript做任何理由吗? 您只能使用CSS执行此操作。

HTML:

<input type="checkbox" />
<label>Check Me!</label>

CSS:

input[type=checkbox] + label {
    color: black;
}
input[type=checkbox]:checked + label {
    background: red;
}

在这里演示


Any reason you need to do this via Javascript? You can do this using CSS only.

HTML:

<input type="checkbox" />
<label>Check Me!</label>

CSS:

input[type=checkbox] + label {
    color: black;
}
input[type=checkbox]:checked + label {
    background: red;
}

DEMO HERE

相关问答

更多