-1

I have a dataframe like this:

A  B  
1 0.1 
1 0.2
1 0.3
2 0.2
2 0.5
2 0.3
3 0.8
3 0.6
3 0.1

How can I find the minimum value belonging to each point 1,2,3 and there should be no conflict which means point 1 and 2 should not belong to same point 0.3..

j.doe
  • 662
  • 4
  • 19
sara
  • 45
  • 2
  • 13

2 Answers2

0

You can use groupby and max function.

df.groupby('A').B.max()
Rarblack
  • 4,559
  • 4
  • 22
  • 33
0

If I understand correctly, you want to do two things: - find the minimum B per distinct A, and - make sure that they don't collide. You didn't specify what to do in case of collision, so I assume you just want to know if there is one.

The first can be achieved with Rarblack's answer (though you should use min and not max in your case). For the second, you can use the .nunique() method - see how many unique B values are there (should be same as number of unique A valuse)

#setup dataframe
df = pd.DataFrame.from_dict({
    'A': [1,1,1,2,2,2,3,3,3],
    'B': [0.1,0.2,0.3,0.2,0.5,0.3,0.8,0.6,0.1]
})

# find minimum
x = df.groupby('A')['B'].min()

# assert that there are no collisions:
if not (x.nunique() == len(x)):
    print ("Conflicting values")
Itamar Mushkin
  • 2,803
  • 2
  • 16
  • 32