1

Let's say that I have this dataframe:

,,,,,,
,,2.0,,,,
,2.0,,2.23606797749979,,,
,,2.23606797749979,,,2.0,
,,,,,2.23606797749979,
,,,2.0,2.23606797749979,,
,,,,,,

enter image description here

I would like to get a two dimensional vector with values of the indexes and the columns of each element which is not nan.

For example, in this case, I am expecting:

[[2,1],[1,2],[3,2],[2,3],[5,3],[3,5],[4,5],[5,4]].

I am thinking about using iloc and the np.where functions but I am not able to merge the two concepts.

1 Answer 1

2

Use DataFrame.stack for remove missing values, if necessary add Series.swaplevel and in list comprehension convert nested tuples to lists:

L = [list(y) for y in df.stack().swaplevel().index]
print (L)
[[2, 1], [1, 2], [3, 2], [2, 3], [5, 3], [5, 4], [3, 5], [4, 5]]

Or if use indices after np.where solution is similar:

r, c = np.where(df.notna())
L = [list(x) for x in zip(c, r)]
print (L)
[[2, 1], [1, 2], [3, 2], [2, 3], [5, 3], [5, 4], [3, 5], [4, 5]]
Sign up to request clarification or add additional context in comments.

2 Comments

Is it possible to eliminate the pairs made by the same couple. I mean, instead of having [2,1] and [1,2] just [1,2]. Maybe another post is more appropriate. What do you think?
@diedro sorry, I am on phone only, possible solution is https://stackoverflow.com/a/15037238

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.