Search code examples
python-3.xpandaslistdataframemin

Python: Fetch value from dataframe list with min value from another column list of same dataframe


input dataframe

Flow     row         count
Apple   [45,46]     [2,1]
Orange  [13,14]     [1,5]

need to find min value of each list column 'count' and fetch respective row value from row column.

Expected output:

  Flow   row     count
  Apple    46     1
  Orange   13     1

Solution

  • In Python, the list has a function called index where you can get the position of the value you want to find. So, by utilizing this function with a min, you can get your desired result.

    df['min_index'] = df['count'].apply(lambda x: x.index(min(x)))
    df[['row_res','count_res']] = [[row[j],count[j]] for row, count, j in zip(df['row'], df['count'], df['min_index'])]
    
         Flow       row   count  min_index  row_res  count_res
    0   Apple  [45, 46]  [2, 1]          1       46          1
    1  Orange  [13, 14]  [1, 5]          0       13          1