Sorting Data
Let me give you the quickest way to organize your data. Sorting in Pandas is intuitive once you know the basics.
Sort by a Single Column
The most common operation — sort by one column:
import pandas as pd
data = {'Name': ['Alice', 'Bob', 'Charlie', 'Diana'],
'Age': [25, 30, 35, 28],
'Score': [88, 92, 79, 95]}
df = pd.DataFrame(data)
print(df.sort_values('Age'))
This sorts by Age in ascending order (lowest to highest). Think of it like clicking the sort button in Excel.
Descending Order
Want highest to lowest? Set `ascending=False`:
print(df.sort_values('Score', ascending=False))
Multiple Sort Keys
Need to sort by more than one column? Pass a list:
print(df.sort_values(['Age', 'Score'], ascending=[True, False]))
This sorts by Age first, then by Score within each age group. The `ascending` list controls each column separately.
Handling Missing Values
One thing that confused me at first was where NaN values end up. Use `na_position` to control this:
print(df.sort_values('Age', na_position='last'))
By default, NaN goes to the bottom. You can set it to 'first' if needed.
Try it Yourself →Key Takeaways
- `sort_values()` sorts by column values
- Use `ascending=False` for descending order
- Pass lists to sort by multiple columns
- `na_position` controls where missing values appear