Exercises about dictionaries, sets, dataframes, arrays and visualizations

Exercises about dictionaries, sets, dataframes, arrays and visualizations#

Dictionaries and sets#

Exercise: Take the list of reactors below and create a dictionary where the keys are the elements in the list (the reactors), and the values are the indexes of the elements in the list. You can do this by using both a for loop or a list comprehension.

Your result should look like this:

  • dict_reactors = {‘batch’: 0, ‘continuous stirred tank’: 1, ‘plug flow’: 2}

Level: Easy.

list_reactors = ['batch', 'continuous stirred tank', 'plug flow']

# Your code here

Now do the same exercise as above, but instead of the index, the values should be properties of the various reactors!

Level: Easy.

# Your code here

Exercise: find the set of the list below.

Level: Easy.

list_reactors = ['batch', 'continuous stirred tank', 'plug flow', 'batch']

# Your code here

Exercise: Find the (i) intersection and (ii) union of the lists given below.

Level: Easy.

reactors1 = ['batch', 'continuous stirred tank']
reactors2 = ['plug flow', 'batch']

# Your code here

list(set(reactors1).intersection(set(reactors2)))
['batch']
list(set(reactors1) & set(reactors2))
['batch']

DataFrames, arrays and plotting#

In the folder, you can load the .csv file ‘packed_column_2.csv’.

Exercise: load the data in pandas, visualize the top 7 rows of the DataFrame, calculate mean, sum, median of the columns and describe the DataFrame.

Level: Easy.

import pandas as pd

# Your code here
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[6], line 1
----> 1 import pandas as pd
      3 # Your code here

ModuleNotFoundError: No module named 'pandas'

Exercise: plot the data, remembering to add a title, axis labels and the legend. Try to make your plot as informative (and pretty) as possible.

Level: Easy.

%matplotlib inline
import matplotlib.pyplot as plt

# Your code here
---------------------------------------------------------------------------
ModuleNotFoundError                       Traceback (most recent call last)
Cell In[7], line 1
----> 1 get_ipython().run_line_magic('matplotlib', 'inline')
      2 import matplotlib.pyplot as plt
      4 # Your code here

File ~\AppData\Local\miniconda3\envs\dig4bio\Lib\site-packages\IPython\core\interactiveshell.py:2480, in InteractiveShell.run_line_magic(self, magic_name, line, _stack_depth)
   2478     kwargs['local_ns'] = self.get_local_scope(stack_depth)
   2479 with self.builtin_trap:
-> 2480     result = fn(*args, **kwargs)
   2482 # The code below prevents the output from being displayed
   2483 # when using magics with decorator @output_can_be_silenced
   2484 # when the last Python token in the expression is a ';'.
   2485 if getattr(fn, magic.MAGIC_OUTPUT_CAN_BE_SILENCED, False):

File ~\AppData\Local\miniconda3\envs\dig4bio\Lib\site-packages\IPython\core\magics\pylab.py:103, in PylabMagics.matplotlib(self, line)
     98     print(
     99         "Available matplotlib backends: %s"
    100         % _list_matplotlib_backends_and_gui_loops()
    101     )
    102 else:
--> 103     gui, backend = self.shell.enable_matplotlib(args.gui)
    104     self._show_matplotlib_backend(args.gui, backend)

File ~\AppData\Local\miniconda3\envs\dig4bio\Lib\site-packages\IPython\core\interactiveshell.py:3665, in InteractiveShell.enable_matplotlib(self, gui)
   3662     import matplotlib_inline.backend_inline
   3664 from IPython.core import pylabtools as pt
-> 3665 gui, backend = pt.find_gui_and_backend(gui, self.pylab_gui_select)
   3667 if gui != None:
   3668     # If we have our first gui selection, store it
   3669     if self.pylab_gui_select is None:

File ~\AppData\Local\miniconda3\envs\dig4bio\Lib\site-packages\IPython\core\pylabtools.py:338, in find_gui_and_backend(gui, gui_select)
    321 def find_gui_and_backend(gui=None, gui_select=None):
    322     """Given a gui string return the gui and mpl backend.
    323 
    324     Parameters
   (...)
    335     'WXAgg','Qt4Agg','module://matplotlib_inline.backend_inline','agg').
    336     """
--> 338     import matplotlib
    340     if _matplotlib_manages_backends():
    341         backend_registry = matplotlib.backends.registry.backend_registry

ModuleNotFoundError: No module named 'matplotlib'

Exercise: import the data we worked with in the theory (‘week02/htory/packed_column.csv’) and compare the two DataFrames using pandas functions. Create a plot (plot can be done with matplotlib) for each water flow in the two dataframes (e.g., one plot with 0 kg/h water flow in column 1 and in column 2).

Level: Medium.

# Your code here

Exercise: convert the two DataFrames to numpy arrays and compare them (e.g., calculate the mean, sum, etc).

Level: Easy.

# Your code here

Exercise: what can you conclude from comparing the flow in the two packed columns? Write some text in markdown.

# write here
# remember to make this a markdown cell

Exercise: explore the data as you want, plot it, investigate it, have fun with it!

# Your code here