Home > Software engineering >  Pandas: Get top n columns based on a row values
Pandas: Get top n columns based on a row values

Time:12-15

Having a dataframe with a single row, I need to filter it into a smaller one with filtered columns based on a value in a row.
What's the most effective way?

df = pd.DataFrame({'a':[1], 'b':[10], 'c':[3], 'd':[5]})
a b c d
1 10 3 5

For example top-3 features:

b c d
10 3 5

CodePudding user response:

Use sorting per row and select first 3 values:

df1 = df.sort_values(0, axis=1, ascending=False).iloc[:, :3]
print (df1)
    b  d  c
0  10  5  3

Solution with Series.nlargest:

df1 = df.iloc[0].nlargest(3).to_frame().T
print (df1)
    b  d  c
0  10  5  3

CodePudding user response:

You can transpose T, and use nlargest():

new = df.T.nlargest(columns = 0, n = 3).T

print(new)

   b  d  c
0  10  5  3
  • Related