Night Walker Night Walker - 11 months ago 61
Python Question

converting dataframe to list of tuples on condition

I have following df:

1 2 3 4
1 NaN 0.000000 0.000000 0.000000
2 NaN 0.027273 0.000000 0.000000
3 NaN 0.000000 0.101449 0.000000
4 NaN 0.000000 0.000000 0.194245
5 NaN 0.000000 0.000000 0.000000
6 NaN 0.000000 0.000000 0.000000
7 NaN 0.000000 0.000000 0.000000
8 NaN 0.000000 0.000000 0.000000
13 NaN 0.000000 0.000000 0.000000
14 NaN 0.000000 5 0.000000


How I can convert it to list of tuples
[(column, row, data)]
and to take only values that are greater then
zero
.

for example I want to have following values:

[(2,2,0.027273), (3,3,0.101449 ), (3,14,5),(4,4,0.194245)]

Answer Source

You can first cast columns to int (if necessary), unstack and use list comprehension, where is necessary convert first and second value in tuples to int (default is float):

df.columns = df.columns.astype(int)

s = df.unstack()
tuples = [tuple((int(x[0]),int(x[1]),x[2])) for x in s[s>0].reset_index().values]
print (tuples)
[(2, 2, 0.027273000000000002), (3, 3, 0.101449), (3, 14, 5.0), (4, 4, 0.194245)]