Introduction to Pandas
Chapter 7: Summary
Exercise
Here is an extra exercise if you wish to practise your pandas skills further.
You will work on a football dataset. Download football.csv
.
The dataset is originally taken from this pandas tutorial.
Once you have downloaded the dataset, complete the tasks below.
- Load
football.csv
file into aDataFrame
and set theTeam
to be the index of theDataFrame
. - Print the number of teams in the
DataFrame
. - Print all the info for all the teams.
- Print the number of
Yellow Cards
andRed Cards
. - Print the teams that scored more than 5 goals.
- Use the
.sort_values()
method to sort the teams that scored more than 5 goals. - Print the teams that had more
Shots on target
thanShots off target
. - Print the teams that got more than 7
Yellow Cards
andRed Cards
combined.
>>> import pandas as pd
>>> df = pd.read_csv("football.csv", index_col=0)
>>> df.shape[0]
>>> df
>>> df[["Yellow Cards", "Red Cards"]]
>>> df[df["Goals"] > 5]
>>> df[df["Goals"] > 5].sort_values("Goals")
>>> df[df["Shots on target"] > df["Shots off target"]]
>>> df[df["Yellow Cards"] + df["Red Cards"] > 7]