JamesHudson81
JamesHudson81

Reputation: 2273

How can I easily iterate at the same time through rows and columns?

I have the following df where if there is a numerical value in a cell, to return the value of the index and the column name:

             A  B  C 

 04/04/18   Nan Nan Nan
 05/04/19   Nan  4  Nan 
 06/04/20   Nan Nan  5 

With the output:

["B-05/04/19","C-06/04/20"]

Is there any simple way I can iterate through rows and columns at the same time without the need of nested loops?

Upvotes: 2

Views: 81

Answers (2)

jezrael
jezrael

Reputation: 862441

If columns and index values are sorted use stack with dropna and last join MulitIndex in list comprehension:

s = df.stack().dropna()
idx = ['{}-{}'.format(b, a) for a, b in s.index]
#python 3.6+
#idx = [f'{b}-{a}' for a, b in s.index]
print (idx)
['B-05/04/19', 'C-06/04/20']

Or get indices of non NaNs values, get values of indexing and join together:

x, y = np.where(df.notnull())
idx = df.columns[y] + '-' + df.index[x]
print (idx)
Index(['B-05/04/19', 'C-06/04/20'], dtype='object')

Upvotes: 2

timgeb
timgeb

Reputation: 78650

Similar to jezrael's solution, but with numpy.argwhere:

>>> idx = np.argwhere(df.notna().values)                                                                                          
>>> ['{}-{}'.format(df.columns[j], df.index[i]) for i, j in idx]                                                                  
['B-05/04/19', 'C-06/04/20']

Upvotes: 0

Related Questions