How to compare the value of rows in a dataframe

Question:

Suppose I have the following dataframe

X Y Z
F1 F2 0
F1 F3 2
F1 F2 1
F1 F4 4
F1 F3 1

and want to convert this into the following

X Y Z
F1 F2 0
F1 F4 4
F1 F3 1

Here i want to remove the rows that have same values in X and Y columns keeping the one whose value in the Z column is lowest. How can I do this?

Asked By: Tania_hussain

||

Answers:

Try this

# index of smallest Z values per X-Y pair
idx = df.groupby(['X', 'Y'])['Z'].idxmin().sort_values()
# filter
df.loc[idx]

enter image description here

Answered By: not a robot
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.