Python Forum
How can i get the top smallest and top highest value for each row in pandas?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can i get the top smallest and top highest value for each row in pandas?
#1
I have data that I have sorted, below is a sample of the data:

Output:
missing_values count 0 51 3 1 12 12 13 1 15 1 16 1 21 1 35 2 36 3 40 1
I have the following code:

# Get the vount of each missing value
missing_value_count = missing_values.iloc[:, 0:1].value_counts().to_frame()
missing_value_count.sort_index(inplace=True)

missing_value_count.to_csv('question.csv')
        
missing_value_count.agg(lambda s: pd.Series([*s.nlargest().index, *s.nsmallest().index],
                           ['missing_values']),
                           axis='columns')
When I run the code I get the following error:

Output:
missing_value_count.agg(lambda s: pd.Series([*s.nlargest().index, *s.nsmallest().index], ['missing_values']), axis='columns') Traceback (most recent call last): Cell In[29], line 1 missing_value_count.agg(lambda s: pd.Series([*s.nlargest().index, *s.nsmallest().index], File D:\Users\Mahmoud\anaconda3\Lib\site-packages\pandas\core\frame.py:9196 in aggregate result = op.agg() File D:\Users\Mahmoud\anaconda3\Lib\site-packages\pandas\core\apply.py:699 in agg result = self.obj.apply(self.orig_f, axis, args=self.args, **self.kwargs) File D:\Users\Mahmoud\anaconda3\Lib\site-packages\pandas\core\frame.py:9423 in apply return op.apply().__finalize__(self, method="apply") File D:\Users\Mahmoud\anaconda3\Lib\site-packages\pandas\core\apply.py:678 in apply return self.apply_standard() File D:\Users\Mahmoud\anaconda3\Lib\site-packages\pandas\core\apply.py:798 in apply_standard results, res_index = self.apply_series_generator() File D:\Users\Mahmoud\anaconda3\Lib\site-packages\pandas\core\apply.py:814 in apply_series_generator results[i] = self.f(v) Cell In[29], line 1 in <lambda> missing_value_count.agg(lambda s: pd.Series([*s.nlargest().index, *s.nsmallest().index], File D:\Users\Mahmoud\anaconda3\Lib\site-packages\pandas\core\series.py:500 in __init__ com.require_length_match(data, index) File D:\Users\Mahmoud\anaconda3\Lib\site-packages\pandas\core\common.py:576 in require_length_match raise ValueError( ValueError: Length of values (2) does not match length of index (1)
I want to return the lowest value in missing_values with the highest values in count. So in the above data the result will be

Output:
missing_values count 0 51
How can I modify this part of the code to get the result I want?

missing_value_count.agg(lambda s: pd.Series([*s.nlargest().index, *s.nsmallest().index],
                           ['missing_values']),
                           axis='columns')
Reply


Messages In This Thread
How can i get the top smallest and top highest value for each row in pandas? - by mahmoudr899 - May-30-2024, 08:06 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Read directory listing of files and parse out the highest number? cubangt 5 5,803 Sep-28-2022, 10:15 PM
Last Post: Larz60+
  Find the highest value of a list Menthix 4 3,050 Oct-29-2021, 02:32 PM
Last Post: Menthix
  Sorting numbers from smallest to biggest Dokugan 2 3,080 Apr-14-2020, 09:24 PM
Last Post: Larz60+
  smallest Cosine distance in Graph vino689 3 3,334 Jan-12-2020, 08:06 AM
Last Post: rmspacedashrf
  Filter only highest version of list with alpanumeric names Haasje 6 4,032 Aug-31-2019, 10:17 AM
Last Post: perfringo
  I want to check for the smallest value entered by the user Shilpa 2 2,701 Aug-13-2019, 12:06 PM
Last Post: ThomasL
  Getting the key for the highest value in a dict - how does this code work? rogfrich 4 4,508 Nov-27-2018, 07:05 PM
Last Post: rogfrich
  How do I calculate the smallest value that is recognized as a difference when compari AFoeee 1 3,739 Oct-28-2018, 10:48 PM
Last Post: Gribouillis

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020