0
0
Pandasdata~5 mins

sort_values() by single column in Pandas - Cheat Sheet & Quick Revision

Choose your learning style9 modes available
Recall & Review
beginner
What does the sort_values() function do in pandas?
It sorts the rows of a DataFrame based on the values in one or more columns.
Click to reveal answer
beginner
How do you sort a DataFrame by a single column named 'age' in ascending order?
Use df.sort_values('age'). This sorts the DataFrame rows by the 'age' column from smallest to largest.
Click to reveal answer
beginner
What parameter do you use to sort a column in descending order with sort_values()?
Set ascending=False inside sort_values() to sort in descending order.
Click to reveal answer
intermediate
What happens if you use inplace=True with sort_values()?
The DataFrame is sorted in place, meaning the original DataFrame changes and nothing is returned.
Click to reveal answer
intermediate
Why might you want to reset the index after sorting a DataFrame?
Because sorting changes the row order but keeps original indexes, resetting the index gives a clean, ordered index.
Click to reveal answer
Which code sorts a DataFrame df by the column 'score' in ascending order?
Adf.sort('score')
Bdf.sort_values('score')
Cdf.sort_values(by='score', ascending=False)
Ddf.order_by('score')
How do you sort a DataFrame by column 'height' in descending order?
Adf.sort_values('height', ascending=False)
Bdf.sort_values('height', ascending=True)
Cdf.sort('height', descending=True)
Ddf.sort_values('height', reverse=True)
What does inplace=True do in sort_values()?
AReturns a new sorted DataFrame
BSorts the DataFrame without changing the original
CRaises an error
DSorts the original DataFrame and returns None
After sorting a DataFrame, why might you call reset_index(drop=True)?
ATo remove the old index and create a new sequential index
BTo sort again
CTo rename columns
DTo filter rows
Which of these is NOT a valid way to specify the column to sort by in sort_values()?
Adf.sort_values('age')
Bdf.sort_values(by='age')
Cdf.sort_values(columns='age')
Ddf.sort_values(['age'])
Explain how to sort a pandas DataFrame by a single column in ascending and descending order.
Think about the parameters of sort_values()
You got /4 concepts.
    Describe why resetting the index after sorting a DataFrame might be useful.
    Consider how DataFrame indexes behave after sorting
    You got /3 concepts.