1

Having a DF as the example below, how can i get list with the index and column name of the values that arent nan?

                  es  ms        hs
subdist_id                        
1                NaN NaN       NaN
2                NaN NaN       NaN
3          -0.218066 NaN -0.309002
4                NaN NaN       NaN
5                NaN NaN       NaN
6                NaN NaN       NaN
7                NaN NaN       NaN
9                NaN NaN       NaN
10               NaN NaN       NaN
11         -0.385217 NaN       NaN

so i can get a list [[3,'es]', [3,'hs'], [11,'es']]

1 Answer 1

3

Use DataFrame.stack for remove NaNs with convert MultiIndex to list of tuples and then convert it to list of lists:

L = list(map(list, df.stack().index.tolist()))
print (L)
[[3, 'es'], [3, 'hs'], [11, 'es']]

Alternative:

L = [list(x) for x in df.stack().index.tolist()]
print (L)
[[3, 'es'], [3, 'hs'], [11, 'es']]

If need performance,get indices by condition by DataFrame.isna and numpy.where and get values by zip with indexing for values of columns and index:

i,c = np.where(df.notna())
L = list(map(list, zip(df.index[i],df.columns[c])))
print (L)
[[3, 'es'], [3, 'hs'], [11, 'es']]

i,c = np.where(df.notna())
L = [list(x) for x in zip(df.index[i],df.columns[c])]
Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.