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

Time:10-14

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

CodePudding user response:

A possible solution (the part .astype('int') may be unnecessary in your case):

df['row'] = list(df.explode(['row', 'count']).reset_index().groupby('flow')
    .apply(lambda x: x['row'][x['count'].astype('int').idxmin()]))

df['count'] = df['count'].map(min)

A shorter solution than my previous one, based on sorted with key:

df.assign(row=df.apply(
 lambda x: sorted(x['row'], key=lambda z: x['count'][x['row'].index(z)])[0],axis=1),
       count=df['count'].map(min))

Output:

     flow  row  count
0   apple   46      1
1  orange   13      1

CodePudding user response:

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
  • Related