0

I have a dataframe that looks like the following:

df
     0    1   2  3   4      
0   0.0 NaN NaN NaN NaN 
1   NaN 0.0 0.0 NaN 4.0 
2   NaN 2.0 0.0 NaN 5.0
3   NaN NaN NaN 0.0 NaN 
4   NaN 0.0 3.0 NaN 0.0

I would like to have a dataframe of all the couples of values different from NaN. The dataframe should be like the following

df
    i   j   val
0   0   0   0.0 
1   1   1   0.0
2   1   2   0.0
3   1   4   5.0
4   3   3   0.0
5   4   1   0.0
6   4   2   3.0
7   4   4   0.0

2 Answers 2

2

Use DataFrame.stack with DataFrame.rename_axis and DataFrame.reset_index:

df = df.stack().rename_axis(('i','j')).reset_index(name='val')
print (df)
    i  j  val
0   0  0  0.0
1   1  1  0.0
2   1  2  0.0
3   1  4  4.0
4   2  1  2.0
5   2  2  0.0
6   2  4  5.0
7   3  3  0.0
8   4  1  0.0
9   4  2  3.0
10  4  4  0.0
Sign up to request clarification or add additional context in comments.

Comments

0

Like this:

In [379]: df.stack().reset_index(name='val').rename(columns={'level_0':'i', 'level_1':'j'})
Out[379]: 
    i  j  val
0   0  0  0.0
1   1  1  0.0
2   1  2  0.0
3   1  4  4.0
4   2  1  2.0
5   2  2  0.0
6   2  4  5.0
7   3  3  0.0
8   4  1  0.0
9   4  2  3.0
10  4  4  0.0

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.