Pandas has the function .nlargest()
. This function takes as parameters:
n
(int
): Number of rows to return
columns
(list
or str
): Column(s) used in sorting
keep
(‘first’
, ‘last’
or False
): Decides what to do with duplicate lines and the default is ‘first’
, that is, maintains the first.
It will return a data frame with the n
first lines of your data frame df
column-ordered columns
. In your case, stay:
df.nlargest(10, 'qtd_ordens')
The same result can be obtained by combining the functions .sort_values()
, which sorts the data frame, and .head()
, selecting the first lines:
df.sort_values(by='qtd_vendas', ascending=False).head(10)
Ai, to select only columns 'nome_cliente'
and 'qtd_vendas'
, stays:
df.nlargest(10, 'qtd_ordens')[['nome_cliente','qtd_ordens']]
Welcome to Stack Overflow! Please explain the problem better, and if possible include a example of code that reproduces what is happening, because your question is not noticeable. See Help Center How to Ask.
– Rebeca Nonato
What have you ever done? Post the code.
– Rebeca Nonato
Also try to clarify the tools you’re using. It’s not just Python, it’s a Data Frame with Pandas. Whenever possible, put examples of expected input and output to help you understand exactly what you intend to do, what you have tried so far, and what the problem is.
– AlexCiuffa