pandas fill a dataframe according to column and row value operations
Question:
Let’s say that I have this dataframe:
,,,,,,
,,2.0,,,,
,2.0,,2.23606797749979,,,
,,2.23606797749979,,,2.0,
,,,,,2.23606797749979,
,,,2.0,2.23606797749979,,
,,,,,,
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.
Answers:
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]]
Let’s say that I have this dataframe:
,,,,,,
,,2.0,,,,
,2.0,,2.23606797749979,,,
,,2.23606797749979,,,2.0,
,,,,,2.23606797749979,
,,,2.0,2.23606797749979,,
,,,,,,
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.
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]]