Pandas Dataframes

Let’s get the top row from a dataframe

Code:

import pandas as pd

df = pd.read_excel('europe_cities_by_sunshine.xlsx')

df = df.sort_values('Year', ascending=False)

df.head(5)

Explanation:

Note when reading in the dataframe, we’re going to sort by the cities with the highest annual sunshine in the Year, and then the df.head() function returns the top of the dataframe. It returns the two cities from Cyprus because we sorted the dataframe, then we picked the top 5 rows — we could get the top 10 rows for ex by doing df.head(10) instead.