简体   繁体   English

如何在pandas数据框中查找具有正值和负值的行

[英]how to find rows with both positive and negative values in pandas dataframe

I have a dataframe like this: 我有一个这样的数据框:

     e_col   in_col     word_col      w_col
     31      9        algorithm    -0.053538
     31      9              ubc    -0.053578
     31      9              kth    -0.053595
     31      8              ubc    -0.053633
     30      8        algorithm     0.043637
     30      7             dale     0.053648
     28      6             dale     0.053671

I want to find the rows in which the same word_col got both positive and negative values in w_col . 我想在word_col找到同一word_col同时具有正值和负值的w_col

So, For example here the output will be: 因此,例如,这里的输出将是:

 31      9        algorithm    -0.053538
 30      8        algorithm     0.043637

Edit 2 : you may also use transform to avoid set_index/reset_index as follows: 编辑2 :您还可以使用transform避免set_index/reset_index ,如下所示:

m = df.w_col.lt(0).groupby(df.word_col).transform('nunique').eq(2)
df.loc[m]

Out[2768]:
   e_col  in_col   word_col     w_col
0     31       9  algorithm -0.053538
4     30       8  algorithm  0.043637

Edit 1 : a shorter way to create m is using nunique() as follows: 编辑1 :创建m一种较短方法是使用nunique() ,如下所示:

m =  df.w_col.lt(0).groupby(df.word_col).nunique().eq(2)

Original : 原件
Do the following: create boolean mask on w_col less than 0 and groupby it by word_col . 请执行下列操作:上创建布尔面具w_col小于0groupby通过它word_col Next, call unique on each group and find any group having len = 2. Use this as the mask to indexing on df.set_index and reset_index back. 接下来,在每个组上调用unique并找到len = 2的任何组。将此掩码用作在df.set_indexreset_index上建立索引的掩码。

m = df.w_col.lt(0).groupby(df.word_col).unique().str.len().eq(2)
df.set_index('word_col').loc[m].reset_index()

Out[2738]:
    word_col  e_col  in_col     w_col
0  algorithm     31       9 -0.053538
1  algorithm     30       8  0.043637

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

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