OpenSesame
Rapunzel Code Editor
DataMatrix
Support forum
Python Tutorials
MindProbe
Python videos

DataMatrix: Exercises and Solutions

from datamatrix import io

dm = io.readxlsx('data/movies.xlsx')
dm = (dm.year >= 1990) & (dm.year < 2000)
for year in dm.year.unique:
    year_dm = dm.year == year
    print('For movies from year {}, the mean rating is {:.2f}'.format(
        year,
        year_dm.rating.mean
    ))

Output:

/home/sebastiaan/anaconda3/envs/pydata/lib/python3.10/site-packages/datamatrix/py3compat.py:105: UserWarning: Some rows miss column title
  warnings.warn(safe_str(msg), *args)
â ¸ Generating...For movies from year 1990, the mean rating is 4.86
For movies from year 1991, the mean rating is 4.99
For movies from year 1992, the mean rating is 5.15
For movies from year 1993, the mean rating is 5.30
For movies from year 1994, the mean rating is 4.91
For movies from year 1995, the mean rating is 4.80
For movies from year 1996, the mean rating is 4.94
For movies from year 1997, the mean rating is 5.12
For movies from year 1998, the mean rating is 5.20
For movies from year 1999, the mean rating is 5.27

An alternative solution makes use of datamatrix.operations.split():

from datamatrix import io
from datamatrix import operations as ops

dm = io.readxlsx('data/movies.xlsx')
dm = (dm.year >= 1990) & (dm.year < 2000)
for year, year_dm in ops.split(dm.year):
    print('For movies from year {}, the mean rating is {:.2f}'.format(
        year,
        year_dm.rating.mean
    ))

Output:

For movies from year 1990, the mean rating is 4.86
For movies from year 1991, the mean rating is 4.99
For movies from year 1992, the mean rating is 5.15
For movies from year 1993, the mean rating is 5.30
For movies from year 1994, the mean rating is 4.91
For movies from year 1995, the mean rating is 4.80
For movies from year 1996, the mean rating is 4.94
For movies from year 1997, the mean rating is 5.12
For movies from year 1998, the mean rating is 5.20
For movies from year 1999, the mean rating is 5.27

You're done with this section!

Continue with DataMatrix >>