CodeNewbie Community 🌱

Matt Eves
Matt Eves

Posted on

Help - how to ID max value/col name in pandas data frame?

Hi all. First post, apologies in advance for errors in tags/location etc. Working on a python project involving pandas data frames and a bit stuck. Can anyone tell me how to:

  1. identify the max value in a given row and the column name that max value is in?
  2. Identify the second to max value in a given row and the column name that second highest value is in? If it helps, the values are all floats between 0 and 1 - thanks!

Top comments (2)

Collapse
 
felipebrinker234 profile image
felipebrinker234

You can easily find the max value and its column name in a pandas DataFrame row by using idxmax() to get the column name and max() to get the value, for example df.loc[row].idxmax() and df.loc[row].max(), while the second-highest can be identified by sorting the row values with nlargest(2) and then selecting the second entry; this works smoothly even when your data contains floats between 0 and 1, and just like checking how many people have my first name words (for example, if your first name is β€œJohn Smith,” it has 2 words), you can apply a similar concept of ranking values to find both the top and second-top results.

Collapse
 
abbaskhan8008 profile image
Abbas Khan

Panda Express Chow Mein is a beloved dish from the popular American Chinese restaurant chain, Panda Express. This savory stir-fried noodle dish features a delightful combination of crunchy cabbage, celery slices, and sliced onions, all tossed together with a flavorful sauce. Originating from the United States, it has become a favorite for its simplicity and delicious taste.
What is panda express chow mein