.. DO NOT EDIT. .. THIS FILE WAS AUTOMATICALLY GENERATED BY SPHINX-GALLERY. .. TO MAKE CHANGES, EDIT THE SOURCE PYTHON FILE: .. "auto_examples/107_filtering.py" .. LINE NUMBERS ARE GIVEN BELOW. .. only:: html .. note:: :class: sphx-glr-download-link-note Click :ref:`here ` to download the full example code .. rst-class:: sphx-glr-example-title .. _sphx_glr_auto_examples_107_filtering.py: Filtering ========= Filtering is often required to reduce the data of a MassComposition object to a specific subset of interest. Both individual objects can be filtered, as can multiple objects contained within a Flowsheet object. .. GENERATED FROM PYTHON SOURCE LINES 10-17 .. code-block:: default import pandas as pd from elphick.mass_composition import MassComposition from elphick.mass_composition.flowsheet import Flowsheet from elphick.mass_composition.datasets.sample_data import sample_data .. GENERATED FROM PYTHON SOURCE LINES 18-22 Create a MassComposition object ------------------------------- We get some demo data in the form of a pandas DataFrame .. GENERATED FROM PYTHON SOURCE LINES 23-27 .. code-block:: default df_data: pd.DataFrame = sample_data() df_data .. raw:: html
wet_mass mass_dry FE SIO2 al2o3 LOI group
index
0 100.0 90.0 57.0 5.2 3.0 5.0 grp_1
1 90.0 80.0 59.0 3.1 1.7 4.0 grp_1
2 110.0 90.0 61.0 2.2 0.9 3.0 grp_2


.. GENERATED FROM PYTHON SOURCE LINES 28-29 Construct a MassComposition object .. GENERATED FROM PYTHON SOURCE LINES 30-34 .. code-block:: default obj_mc: MassComposition = MassComposition(df_data, name='demo') obj_mc.data.to_dataframe() .. raw:: html
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group
index
0 100.0 90.0 10.000000 57.0 5.2 3.0 5.0 grp_1
1 90.0 80.0 11.111111 59.0 3.1 1.7 4.0 grp_1
2 110.0 90.0 18.181818 61.0 2.2 0.9 3.0 grp_2


.. GENERATED FROM PYTHON SOURCE LINES 35-40 Filtering Single Objects ------------------------ One of the most common subsets is one that contains records above a particular grade. The method used to filter is called query, for consistency with the xarray and pandas methods that execute the same. .. GENERATED FROM PYTHON SOURCE LINES 41-45 .. code-block:: default obj_1: MassComposition = obj_mc.query({'index': 'Fe>58'}) obj_1.data.to_dataframe() .. raw:: html
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI group
index
1 90.0 80.0 11.111111 59.0 3.1 1.7 4.0 grp_1
2 110.0 90.0 18.181818 61.0 2.2 0.9 3.0 grp_2


.. GENERATED FROM PYTHON SOURCE LINES 46-47 Notice that the record with an Fe value below 58 has been removed. .. GENERATED FROM PYTHON SOURCE LINES 49-53 Filtering Multiple Objects -------------------------- Multiple objects can be loaded into a Flowsheet. We'll make a small network to demonstrate. .. GENERATED FROM PYTHON SOURCE LINES 54-59 .. code-block:: default obj_one, obj_two = obj_mc.split(fraction=0.6, name_1='one', name_2='two') fs: Flowsheet = Flowsheet.from_streams([obj_mc, obj_one, obj_two], name='Network') .. GENERATED FROM PYTHON SOURCE LINES 60-62 The weighted mean mass-composition of each object/edge/stream in the network can be reported out with the report method. .. GENERATED FROM PYTHON SOURCE LINES 62-65 .. code-block:: default fs.report() .. raw:: html
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI
name
demo 300.0 260.0 13.333333 59.0 3.515385 1.873077 4.0
one 180.0 156.0 13.333333 59.0 3.515385 1.873077 4.0
two 120.0 104.0 13.333333 59.0 3.515385 1.873077 4.0


.. GENERATED FROM PYTHON SOURCE LINES 66-67 Now we'll filter as we did before, though we must specify which object the query criteria is to be applied to. .. GENERATED FROM PYTHON SOURCE LINES 67-69 .. code-block:: default fs.query(mc_name='demo', queries={'index': 'Fe>58'}).report() .. raw:: html
mass_wet mass_dry H2O Fe SiO2 Al2O3 LOI
name
demo 200.0 170.0 15.0 60.058824 2.623529 1.276471 3.470588
one 120.0 102.0 15.0 60.058824 2.623529 1.276471 3.470588
two 80.0 68.0 15.0 60.058824 2.623529 1.276471 3.470588


.. rst-class:: sphx-glr-timing **Total running time of the script:** ( 0 minutes 0.232 seconds) .. _sphx_glr_download_auto_examples_107_filtering.py: .. only:: html .. container:: sphx-glr-footer sphx-glr-footer-example .. container:: sphx-glr-download sphx-glr-download-python :download:`Download Python source code: 107_filtering.py <107_filtering.py>` .. container:: sphx-glr-download sphx-glr-download-jupyter :download:`Download Jupyter notebook: 107_filtering.ipynb <107_filtering.ipynb>` .. only:: html .. rst-class:: sphx-glr-signature `Gallery generated by Sphinx-Gallery `_