0

I have created a panda df with one row and 1700 columns. Now I have a specific value say 10 that is present in one of these 1700 columns. How do I retrieve that column name with the specific value =10?

Dat21
  • 11
  • 1
  • `df.iloc[0].eq(10).idxmax()`? – mozway Jul 08 '22 at 07:53
  • Is guaranteed always exist value `10` ? If not ouput should be error, None, something else? E.g. if test value 1000 - this value not exist in any column. – jezrael Jul 08 '22 at 07:58
  • I understood that the value is indeed present: "***I have a specific value** say 10 **that is present** in one of these 1700 columns*". Otherwise it's possible to check that `df.iloc[0][found_idx]` is indeed `10` – mozway Jul 08 '22 at 08:00
  • added to answer what I think. – jezrael Jul 08 '22 at 08:02

2 Answers2

0

If exist value at least one value solution from comment working well - get first match:

df = pd.DataFrame([[1,1,10,5,7]]).add_prefix('col')
print (df)
   col0  col1  col2  col3  col4
0     1     1    10     5     7

Select first row for Series, compare 10 and use Series.idxmax:

col = df.iloc[0].eq(10).idxmax(axis=1)
print (col)
col2

If test and not exist got wrongly first column, not bug, but feature:

col = df.iloc[0].eq(100).idxmax(axis=1)
print (col)
col0

Possible solution is test if exist at least one value - here is None instead first column:

m = df.iloc[0].eq(100)
col = m.idxmax(axis=1) if m.any() else None
print (col)
None
jezrael
  • 822,522
  • 95
  • 1,334
  • 1,252
0

Assuming the value exists, the simplest in my opinion would be to use df.iloc[0].eq(10).idxmax()

If there is a chance that the value is absent and you want to avoid returning the first value, use:

s = df.iloc[0]
s[s.eq(10)].first_valid_index()
# 'col2'   # using @jezrael's example

s[s.eq(9)].first_valid_index()
# no output
mozway
  • 194,879
  • 13
  • 39
  • 75