Suppose I have dataframe like
id p1 p2 p3 p4
1 0 9 0 4
2 0 0 0 4
3 1 3 10 7
4 1 5 3 1
5 2 3 7 10
Want to find column names of top-n highest-value columns in each pandas data frame row and want to exclude zero value from top 3.
id top1 top2 top3
1 p2 p4
2 p4
3 p3 p4 p2
4 p2 p3 p4/p1
5 p4 p3 p2
The present solutions return column names which are having zero too. Is there way to exclude zero values. have this solution
arank = df.apply(np.argsort, axis = 1)
ranked_cols = df.columns.to_series()[arank.values[:,::-1][:,:3]]
new_df = pd.DataFrame(ranked_cols, index=df.index)
there also other solutions such as Find names of top-n highest-value columns in each pandas dataframe row. Can these be modified to exclude columns with zero value?