Search code examples
pythonpandasdrop

Pandas compare columns and drop rows based on values in another column


Is there a way to drop values in one column based on comparison with another column? Assuming the columns are of equal length

For example, iterate through each row and drop values in col1 greater than values in col2? Something like this:

df['col1'].drop.where(df['col1']>=df['col2']


Solution

  • Pandas compare columns and drop rows based on values in another column

    import pandas as pd
    
    d = {
        '1': [1, 2, 3, 4, 5],
        '2': [2, 4, 1, 6, 3]
    }
    
    df = pd.DataFrame(d)
    print(df)
    
    dfd = df.drop(df[(df['1'] >= df['2'])].index)
    print('update')
    print(dfd)
    

    Output

       1  2
    0  1  2
    1  2  4
    2  3  1
    3  4  6
    4  5  3
    update
       1  2
    0  1  2
    1  2  4
    3  4  6