This is an archived version of the course. Please find the latest version of the course on the main webpage.

Chapter 7: Summary

Exercise

face Oana Cocarascu

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.

  1. Load football.csv file into a DataFrame and set the Team to be the index of the DataFrame.
  2. Print the number of teams in the DataFrame.
  3. Print all the info for all the teams.
  4. Print the number of Yellow Cards and Red Cards.
  5. Print the teams that scored more than 5 goals.
  6. Use the .sort_values() method to sort the teams that scored more than 5 goals.
  7. Print the teams that had more Shots on target than Shots off target.
  8. Print the teams that got more than 7 Yellow Cards and Red 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]