简体   繁体   English

熊猫数据框比较

[英]Pandas Dataframe Comparisons

I am learning Pandas dataframes for a project and having trouble understanding some of the operators and how I can use them. 我正在为一个项目学习Pandas数据框,但在理解某些操作员以及如何使用它们方面遇到困难。 In one case, I have one dataframe for production data and another for targets. 在一种情况下,我有一个数据框用于生产数据,另一个用于目标。 I can get the items in the production data that met the targets and those that didn't using: 我可以在生产数据中获得满足目标和未使用的项目:

good = prod['A'][prod['A'] >= target['A']]
bad = prod['A'][prod['A'] < target['A']]

and it works well. 而且效果很好。 But in some cases, I have an upper and lower target, which is where I am getting stuck. 但是在某些情况下,我有一个上限和下限目标,这就是我陷入困境的地方。 I need to find the values that are above the upper target, the values below the lower target and the values that were in between and get 3 separate dataframes. 我需要找到位于较高目标上方的值,位于较低目标下方的值以及介于两者之间的值,并获得3个单独的数据框。 I tried what seemed obvious working with normal lists: 我尝试了使用普通列表似乎很明显的方法:

aboveTargetA = prod['A'][prod['A'] >= targetA['A']]
belowTargetB = prod['A'][prod['A'] <= targetB['A']]
betweenTargets = prod[[col for index, col in df.iterrows() if col not in aboveTargetA and col not in belowTargetB]]

I'm not sure how I should be doing it with these dataframes and generators as I have never worked with them before. 我不确定如何使用这些数据框和生成器,因为我以前从未使用过它们。 Can anyone point me in the right direction for the comparisons? 谁能指出我进行比较的正确方向?

You can do boolean indexing with multiple conditions: 您可以使用多个条件进行布尔索引:

prod['A'][(prod['A'] < targetA['A']) & (prod['A'] > targetB['A'])]

See also http://pandas.pydata.org/pandas-docs/dev/indexing.html#boolean-indexing 另请参见http://pandas.pydata.org/pandas-docs/dev/indexing.html#boolean-indexing

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM