简体   繁体   中英

How to delete rows from column 1 of dataframe based on condition from column 2?

I have seen a lot of answers on how to delete dataframe rows based on a condition.

However, I have a python dataframe with X and Y values.

I want to delete X values in range 3 to 10 and 33 to 40 when y is in range 3 to 37.

How can we do this?

This is what the dataframe looks like:

               X          Y         Z         
0      34.587329   0.448762  0.572807
1      34.587325   0.700001  0.579152  
2      34.587327   0.950507  0.578303  
3      34.587328   1.199202  0.579037  
4      34.587328   1.445252  0.581698  
...          ...        ...       ...              
20956   0.013592  41.207004  0.585673  
20957   0.013495  40.411135  0.554721  
20958   0.013909  41.701004  0.571247  
20959   0.014094  41.962998  0.578334  
20960   0.014193  42.208740  0.555786  

use Series.between

hold_x = ~(df['X'].between(3, 10) | df['X'].between(33, 40))
hold_y = ~df['Y'].between(3, 37)


df['X'] = df['X'].where(hold_x | hold_y)

You could also use isin + range() . the difference is that in this way you can include only the left limit:

df['X'].isin(range(3, 10))

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