过滤三列中的值相同的数据

时间:2019-11-29 14:57:25

标签: python pandas

我正在将CSV导入Python,并且希望过滤该列以仅显示具有4个与EG一致的值的行

ID,  Name,  Performance test 1 , Performance test 2 , Performance test 3, Performance test 4, Consistent?
1    Bob    Pass                 Pass                 Pass                 Pass               TRUE 
2    Dave   Pass                 Fail                 Pass                 Pass               FALSE
3    Roger  Fail                 Fail                 Fail                 Fail               TRUE 

2 个答案:

答案 0 :(得分:1)

DataFrame.filter1进行比较,以测试DataFrame.nunique每行的所有值是否唯一:

df['Consistent?'] = df.filter(like='Performance test').nunique(axis=1).eq(1)
print (df)

   ID   Name Performance test 1 Performance test 2 Performance test 3  \
0   1    Bob               Pass               Pass               Pass   
1   2   Dave               Pass               Fail               Pass   
2   3  Roger               Fail               Fail               Fail   

  Performance test 4  Consistent?  
0               Pass         True  
1               Pass        False  
2               Fail         True  

为了提高性能,将第一个过滤列的所有值与DataFrame.all进行比较,以测试每行是否有所有True

df1 = df.filter(like='Performance test')
df['Consistent?'] = df1.eq(df1.iloc[:, 0], axis=0).all(axis=1)

答案 1 :(得分:0)

这种方法虽然更长一些,但它是显式的,可以帮助您根据需要进行修改。比较“性能测试2”,“性能测试3”和“性能测试4”列中的值与“性能测试1”中的值是否相等。

    import pandas as pd
    import numpy as np

    data = {'ID':[1,2,3], 'Name':['Bob', 'Dave', 'Roger'], 'Performance Test 1':["Pass", "Pass", "Fail"], 'Performance Test 2':["Pass", "Fail", "Fail"], 'Performance Test 3':["Pass", "Pass", "Fail"], 'Performance Test 4':["Pass", "Pass", "Fail"]} 

    df = pd.DataFrame(data)

    df['Consistent?'] = np.where((df['Performance Test 1'] == df['Performance Test 2']) & (df['Performance Test 1'] == df['Performance Test 3']) & (df['Performance Test 1'] == df['Performance Test 4']), 1, 0)
    df.head()
        Out[9]: 
   ID   Name Performance Test 1 Performance Test 2 Performance Test 3  \
0   1    Bob               Pass               Pass               Pass   
1   2   Dave               Pass               Fail               Pass   
2   3  Roger               Fail               Fail               Fail   

  Performance Test 4  Consistent?  
0               Pass            1  
1               Pass            0  
2               Fail            1