Reputation: 3146
I am trying to extract the top % of the last row value in dataframe.
df=pd.DataFrame.from_dict(
{
'u':[6,7,11],
'v':[4,7,1],
'w':[0,4,7],
'x':[1,3,5],
'y':[2,1,6],
'z':[3,9,8],
})
pct=40 #40%
val=int((pct*.01)*len(df.columns))
s=df.iloc[-1].nlargest(val)
print (df[s.keys())
I was wondering if this is the correct way.
Upvotes: 0
Views: 68
Reputation: 863741
I believe you need:
pct=40 #40%
val=int((pct*.01)*len(df.columns))
df = df.loc[:, df.iloc[-1].nlargest(val).index]
print (df)
u z
0 6 3
1 7 9
2 11 8
Solution with numpy.argsort
for positions by sorted data, select last columns in inverse order and pass to iloc
:
pct=40 #40%
val=int((pct*.01)*len(df.columns))
df = df.iloc[:, df.iloc[-1].argsort()[:-val - 1:-1]]
print (df)
u z
0 6 3
1 7 9
2 11 8
Upvotes: 1