Python熊猫:如何使用' where'和' isin'有两个条件?

时间:2015-04-24 08:56:38

标签: python pandas group-by conditional-statements

我有一个数据框' dfm' :

      match_x            org_o           group   match_y
0       012  012 Smile Communications     92      012
1       012                 012 Smile     92      000
2   10types                   10TYPES     93      10types
3   10types               10types.com     97      10types
4  360works                  360WORKS     94      360works
5  360works              360works.com     94      360works

我想要一个专栏来' a'叫做标签'。对于dfm中的每个组织,当match_x和match_y相等且他们有一个唯一的组时,标签将是' TP'否则它就是' FN'。这是我使用的代码:

a['tag'] = np.where(((a['match_x'] == a['match_y']) & (a.groupby(['group', 'match_x','match_y'])['group'].count() == 1)),'TP', 'FN')

但是我收到了这个错误:

TypeError: 'DataFrameGroupBy' object is not callable

有人知道怎么做吗?

1 个答案:

答案 0 :(得分:2)

让我们稍微分析你的巨大声明:

a['tag'] = np.where(((a['match_x'] == a['match_y']) & (a.groupby(['group', 'match_x','match_y'])['group'].count() == 1)),'TP', 'FN')

解除面具:

mask = ((a['match_x'] == a['match_y']) & (a.groupby(['group', 'match_x','match_y'])['group'].count() == 1))
a['tag'] = np.where(mask,'TP', 'FN')

打破面具:

mask_x_y_equal = a['match_x'] == a['match_y']
single_line = a.groupby(['group', 'match_x','match_y']).size() == 1
mask = (mask_x_y_equal & single_line)
a['tag'] = np.where(mask,'TP', 'FN')

如果你这样做,错误将更加明显。 single_line掩码与mask_x_y_equal的长度不同。 这成为一个问题,因为& sign不关心系列的索引,这意味着你现在有一个无声错误。

我们可以通过在数据框内操作来删除此无提示错误:

df_grouped = a.groupby(['group', 'match_x','match_y']).size() # size does what you do with the ['group'].count(), but a bit more clean.
df_grouped.reset_index(inplace=True) # This makes df_grouped into a dataframe by putting the index back into it.
df_grouped['equal'] = df_grouped['match_x'] == df_grouped['match_y'] # The mask will now be a part of the dataframe

mask = (df_grouped['equal'] & (df_grouped['0'] == 1)) # Now we create your composite mask with comparable indicies
a['tag'] = np.where(mask, 'TP', 'FN')

这可能会或可能不会解决您的" TypeError:' DataFrameGroupBy'对象不可调用"。无论哪种方式,将您的陈述分解为多行都会向您显示更多错误。