Skip to content Skip to sidebar Skip to footer

Leaving Rows With A Giving Value In Column

UPDATED: In my dataset I have 3 columns (x,y) and VALUE. It's looking like this(sorted already): df1: x , y ,value 1 , 1 , 12 2 , 2 , 12 4 , 3 , 12 1 , 1 , 11 2 , 2 , 11 4 , 3 , 11

Solution 1:

From what I understand, the order in which you make your operations (filter those with distance <= 1 and grouping them) has no importance.

Here is my take:

#first selection of the lines with right distance
filtered_df = df[df.apply(lambda line: abs(line['x']- line['y']) <= 1, 1)]

# Then group
for i in filtered_df.groupby('value'):
     print(i)
     # Or do whatever you want

Let me know if you want some explanations on how some part of the code works.


Post a Comment for "Leaving Rows With A Giving Value In Column"