How to sort an index of numbers with Pandas in Python
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
In order to sort an index made of numbers, we can use the DataFrame.sort_index() method like in the example below.
# We import pandas
import pandas as pd
# We generate our sample dataset
df = pd.DataFrame(index=sorted(list(range(10)), reverse=True),
data={"col1":range(10)})
print(df)
# By ascending order
print(df.sort_index())
# By descending order
print(df.sort_index(ascending=False))
Here you are! You now know how to sort an index of numbers with Pandas in Python!
More on DataFrames
If you want to know more about DataFrame and Pandas. Check out the other articles I wrote on the topic, just here :
Land Your First Data Science Job
A proven roadmap to prepare for $75K+ entry-level data roles. Perfect for Data Scientist ready to level up their career.
Related Articles
Continue your learning journey with these related topics
Master Data Science in Days, Not Months 🚀
Skip the theoretical rabbit holes. Get practical data science skills delivered in bite-sized lessons – Approach used by real data scientist. Not bookworms. 📚