I'm iterating over a dataframe, and I'm looking to retrieve the column names of every single row that has cells matching a certain value. For example, let's say row 1 has 100 columns. From those 100 columns, 30 contain the value I'm looking for. I want the names of the all columns that contained that value. Here is what I've done so far:
for _, temp in df.iterrows():
print(temp[(temp == 1.0)])
Here's a sample output of the previous print statement, where 1.0 is the value I'm interested in.
Product 1 1.0
Product 2 1.0
Product 3 1.0
Product 4 1.0
Product 5 1.0
Name: 1, dtype: float64
The problem that I'm facing is that I am unable to store those names in a list. I've tried:
for _, temp in df.iterrows():
temp_1 = temp[(temp == 1.0)]
print(temp_1.columns.values)
but I get AttributeError: 'Series' object has no attribute 'columns'
Please help. Thanks in advance!