Home > Back-end >  Pandas - return value of column
Pandas - return value of column

Time:01-03

I have a df with categories and thresholds:

cat t1 t2 t3 t4
a   2  4  6  8
b   3  5  7  0
c   0  0  1  0

My end goal is to return the column name given category and score. I can select a row using a cat variable:

df[df['cat'] == cat]

How do I now return the column name that is closest to the score (rounded down)? (c, 3) -> t3

CodePudding user response:

Try this:

>>> df
  cat  t1  t2  t3  t4
0   a   2   4   6   8
1   b   3   5   7   0
2   c   0   0   1   0

>>> category = 'c'
>>> score = 3
>>> df.set_index('cat').loc[category, f't{score}']
1

>>> category = 'b'
>>> score = 2
>>> df.set_index('cat').loc[category, f't{score}']
5

CodePudding user response:

You can compute the absolute difference to your value and get the index of the minimum with idxmin:

value = 3
cat = 'c'

(df.set_index('cat')
   .loc[cat]
   .sub(value).abs()
   .idxmin()
 )

Output: 't3'

  • Related