大熊猫将多个列与数据框中的特定列进行比较

时间:2019-12-16 12:08:58

标签: python pandas

我有一个像这样的数据框:

let someSet: Set<Int> = [1, 3, 5, 7]
print(someSet.map { $0 + 1 })

// Prints:  [2, 6, 8, 4]

如果列Store_2_qty,Store_3_qty都等于Store_1_qty,我想再增加一列“ true”或“ false”。但是有时有时会添加诸如Store_4_qty,Store_5_qty之类的额外列,为此,我再次需要将所有列与Store_1_qty进行比较

我尝试了这个,但是它在最后一列中返回了所有False

Product_ID Store_1_qty Store_2_qty Store_3_qty
A          10          20          10
B          10          10          10
C          10          10          20

3 个答案:

答案 0 :(得分:2)

如果.Migrate()是使用DataFrame.eq的列,那么您也可以将所有列与第一Product_ID列进行比较:

Store

答案 1 :(得分:0)

如果我理解正确,那么您要做的就是测试df条目的唯一性:

v <- c(1, 2, 3, 4, 5, 6, 7, 734, 456, 346, 545, 874, 734, 455, 734, 
783, 482, 545, 456, 948, 483)

结果:

# Assuming Product_ID is NOT your index:
df['match'] = df.iloc[:,2:].nunique(axis=1) == 1

# If Product_ID is your index it simplifies to:
df['match'] = df.nunique(axis=1) == 1

答案 2 :(得分:0)

只是尝试。 @jezrael的解决方案更好

df['match'] = df[['Store_2_qty', 'Store_3_qty']].eq(df['Store_1_qty'],axis=0).all(1)

输出:

  Product_ID  Store_1_qty  Store_2_qty  Store_3_qty  match
0          A           10           20           10  False
1          B           10           10           10   True
2          C           10           10           20  False