pandas 如何根据布尔标准拆分数据帧?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/14957116/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-13 20:39:39  来源:igfitidea点击:

How to split a dataframe according to a boolean criterion?

pandas

提问by kjo

Suppose that dfis a pandasdataframe. I want to split it into two dataframes according to some criterion. The best way I've found for doing this is something like

假设这df是一个pandas数据框。我想根据某种标准将它分成两个数据帧。我发现这样做的最好方法是

df0, df1 = [v for _, v in df.groupby(df['class'] != 'special')]

In the above example, the criterion is the argument to the groupbymethod. The resulting df0consists of the sub-dataframe where the classfield hasvalue 'special', and df1is basically the complement of df0. (Unfortunately, with this construct, the sub-dataframe consisting of the items that failthe criterion are returned first, which is not intuitive.)

在上面的例子中,标准是groupby方法的参数。结果df0class字段具有value的子数据帧组成'special',并且df1基本上是 的补充df0。(不幸的是,使用此构造,首先返回由符合标准的项目组成的子数据框,这不直观。)

The above construct has the drawback that it is not particularly readable, certainly not as readable as, for instance, some hypothetical splitbymethod like

上述构造的缺点是它不是特别可读,当然不如某些假设splitby方法如

df0, df1 = df.splitby(df['class'] == 'special')

Since splitting a dataframe like this is something I often need to do, I figure that there may be a built-in function, or maybe an established idiom, for doing this. If so, please let me know.

由于像这样拆分数据帧是我经常需要做的事情,我认为可能有一个内置函数,或者可能是一个既定的习惯用法,用于执行此操作。如果是这样,请告诉我。

回答by Andy Hayden

I think the most readable way is to do this is:

我认为最易读的方法是:

m = df['class'] != 'special'
a, b = df[m], df[~m]

I haven't come across a special method for this...

我还没有遇到过一种特殊的方法...