简体   繁体   中英

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']

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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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