Home > Software engineering >  Get Top 11 rows with Non Zeros column in Pandas Dataframe
Get Top 11 rows with Non Zeros column in Pandas Dataframe

Time:10-05

I have a pandas data frame that looks like the following. It has 11 column that contains only zeros and ones, along with a column with some values and the last column is an identifier. I am facing a problem with data frame manipulation.

I have a situation where I need to select the top 11 rows based on the 'values' column. (Loose Constraint)

But the tricky part is I have to select the rows in such a way that I do not get any zero columns in those eleven rows. (Hard Constraint).

So I need to select to top 11 rows based on values and make sure all columns are non-zero. At least one value in each column should be 1.

I am looking for some generic solution as the values in the Value column will change but my goal of selecting 11 rows based on value and making sure a non-zero column is a must.

Any ideas?

a    b  c   d   e   f   g   h   i   j   k      values ID
0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 1.193744   1
0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 1.193744   2
0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 1.193744   3
0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 1.193744   4
0.0 0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.193744   5
0.0 0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.193744   6
0.0 0.0 0.0 1.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.193744   7
0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.633150   8
0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.633150   9
0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.633150  10
0.0 0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.633150  11
0.0 0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.633150  12
0.0 0.0 1.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.033640  13
0.0 0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.033640  14
0.0 1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.033640  15
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.033640  16
0.0 1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.033640  17
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.033640  18
0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.033640  19
1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.033640  20
0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.033640  21
0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.033640  22
0.0 1.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 0.033640  23
0.0 1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.033640  24
1.0 0.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.033640  25
1.0 0.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 -0.279495 26
1.0 0.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 -3.013531 27
1.0 0.0 0.0 0.0 1.0 0.0 0.0 0.0 0.0 0.0 0.0 -3.013531 28

CodePudding user response:

Try:

print(df[(df>1).any(axis=1)].head(11))

CodePudding user response:

@Muhammadhassan did it right, in addition to get the order :

df.loc[(df>=1).any(axis=1)].sort_values(by='values', ascending=False).head(11)
  • Related