Given a dataframe like below
cat dog hamster dolphin
cat 1 0.5 0 0.25
dog 0.5 1 0 0
hamster 0 0 1 0.5
dolphin 0.25 0 0.5 1
I want to get the column values which are bigger than zero for the given row in dictionary format. For example, for hamster line, the result should be:
{ 'hamster': 1, 'dolphin': 0.5 }
It would be even better omitting the column with the same name though, so for 'hamster', this would be better:
{ 'dolphin': 0.5 }
At the moment I receive all values of the given row using df["hamster"].to_dict() and removing zero values with dictionary comprehension, like {k: v for (k,v) in d.items() if v > 0 }, but it's far from ideal, as in the original size of dataframe is about 50000 x 50000. Is there any simpler method in pandas to filter out the columns with value 0 (and the column with the same name, if it's easy to do)?