I have a dataframe that looks like
userId feature1 feature2 feature3 ...
123456 0 0.45 0 ...
234567 0 0 0 ...
345678 0.6 0 0.2 ...
.
.
The features are mostly zeros but occasionally some of those would have non-zero values. A single row for a userId may have zero, one or more non-zero features.
I want to transform this into the following dataset:
userId feature value
123456 feature2 0.45
345678 feature1 0.6
345678 feature3 0.2
Essentially, we retain only the features that are non-zero for each userId. So, for userId 345678, we have 2 rows in the transformed dataset, one for feature1 and the other for feature3. userId 234567 is dropped since none of the features are non-zero.
Is this something that can be done using groupby or pivoting? If so, how?
Any other pandas-mic solutions?