Reputation: 13
My question is extension of this question. Although the question asked are same, answers are only applicable to very specific situation.
Assuming same data set, I want to add column with column name containing specific value
Date col1 col2 col3 col4 col5 col6 col7
01/01/2016 00:00 37.04 36.57 35.77 37.56 36.79 35.90 38.15
However, I have large number of rows, and the value I want to find is neither max nor min (say i want to col name with value 36.79).
Upvotes: 1
Views: 67
Reputation: 862481
You can compare all values of DataFrame
and test at least one True
by DataFrame.any
- then mask is used for filter columns names:
c = df.columns[(df == 36.79).any()].tolist()
But because working with floats and possible some accuracy problems is better use numpy.isclose
with select only numeric columns:
#if possible some non numeric columns
df1 = df.select_dtypes(np.number)
c = df1.columns[np.any(np.isclose(df1, 36.79), axis=0)].tolist()
#if all columns are numeric
c = df.columns[np.any(np.isclose(df, 36.79), axis=0)].tolist()
Sample:
print (df)
Date col1 col2 col3 col4 col5 col6 col7
0 01/01/2016 00:00 37.04 36.57 35.77 37.56 36.79 35.9 38.15
1 01/02/2016 00:00 37.04 36.57 35.77 37.56 36.79 35.9 36.79 <-last value changed
df1 = df.select_dtypes(np.number)
c = df1.columns[np.any(np.isclose(df1, 36.79), axis=0)].tolist()
print (c)
['col5', 'col7']
Last if want first matched value if exist is possible use next
with iter
for possible pass default value if empty list (no value match):
print (next(iter(c), 'no match'))
col5
df1 = df.select_dtypes(np.number)
c = df1.columns[np.any(np.isclose(df1, 100), axis=0)].tolist()
print (c)
[]
print (next(iter(c), 'no match'))
no match
Upvotes: 1