Reputation: 51
so I have a data frame with 187 columns. The last column(called target) is in 1's and 0's indicating True or False. I would like to group by all the rows accordingly to the last column if it has 1 and all the rows to 0 if they equal out to 0 in the last column. 1 being shown on top and 0 after the 1's. Also, would it be possible to take out the index value of the last "1" in the last column?
Apologies if this is a bit confusing or if it has already been answered but I can't seem to find anything. There are about 18000 rows with approx. 14000 values equal to 1's and the rest of the 4000 equal to 0's. Would appreciate some sort of guidance here.
Upvotes: 1
Views: 44
Reputation: 862511
First sorting values by mergesort
by column target
and get last row by target
by DataFrame.drop_duplicates
:
df1 = df.sort_values('target', ascending=False, kind='mergesort')
last = df1.drop_duplicates('target', keep='last')
If need grouping:
for name, g in df1.groupby('target'):
print (name)
print (g)
Upvotes: 1