0

I want to print out the highest, not unique value out of my dataframe. With df['Value'].value_counts() i can count them, but how do i selected them by how often the numbers appear.

Value
1
2
1
2
3
2
  • You might want to refer to this https://stackoverflow.com/questions/48590268/pandas-get-the-most-frequent-values-of-a-column/48590361 – CompEng007 Oct 01 '21 at 06:43
  • My table is a bad example because 2 is in my case the second highest value and also the most frequent one. In that case your answere would help. But if 1 is the most frequent one and 3 the highest there is no solution on the question you linked. – AchillesDay Oct 01 '21 at 07:01
  • You want the answer to be 3 in that case because it is the maximum value, right? – CompEng007 Oct 01 '21 at 07:22
  • i want to check if the highest value is more then once in the dataframe, if so i just want to print it. If not i want to check if the second highest value is more then once in my dataframe. – AchillesDay Oct 01 '21 at 07:40
  • I think I understand. You can see my answer below. – CompEng007 Oct 01 '21 at 08:01

1 Answers1

0

As I understand you want the first highest value that has a frequency greater than 1. In this case you can write,

for val, cnt in df['Value'].value_counts().sort_index(ascending=False).iteritems():
  if cnt > 1:
    print(val)
    break

The sort_index sorts the items by the 'Value' rather than the frequencies. For example if your 'Value' columns has values [1, 2, 3, 3, 2, 2,2, 1, 3, 2] then the result of df['Value'].value_counts().sort_index(ascending=False).iteritems() will be as follows,

3    3
2    5
1    2
Name: Value, dtype: int64

The answer in this example would then be 3 since it is the first highest value with frequency greater than 1.

CompEng007
  • 86
  • 8