<jupyter_start><jupyter_text>Module 5 Video 25: Filtering by Vessel**Python for the Energy Industry**[Vessels endpoint documentation.](https://vortechsa.github.io/python-sdk/endpoints/vessels/)By now, you should be well used to the process of filtering Cargo Movements, and searching the relevant endpoints to get IDs of interest for filtering. This works in exactly the same way for filtering Cargo Movements that are being carried on a known Vessel:<jupyter_code># Some initial imports / settings
from datetime import datetime
from dateutil.relativedelta import relativedelta
import vortexasdk as v

now = datetime.utcnow()
one_month_ago = now - relativedelta(months=1)

required_columns = ["vessels.0.name","vessels.0.vessel_class","product.group.label","product.grade.label","quantity",
                    "status","events.cargo_port_load_event.0.location.port.label","events.cargo_port_load_event.0.end_timestamp",
                    "events.cargo_port_unload_event.0.location.port.label","events.cargo_port_unload_event.0.end_timestamp"]

new_labels = ["vessel_name","vessel_class","product_group","product_grade","quantity","status","loading_port","loading_finish","unloading_port","unloading_finish"]

relabel = dict(zip(required_columns,new_labels))
# get ID for all vessels with 'CRIMSON' in the name
crimson_id = [ves['id'] for ves in v.Vessels().search(term='CRIMSON')]

# filter for cargo movements on CRIMSON
crimson_cm = v.CargoMovements().search(
    filter_activity="loading_state",
    filter_time_min=one_month_ago,
    filter_time_max=now,
    filter_vessels=crimson_id)

crimson_cm.to_df(columns=required_columns).rename(relabel,axis=1)<jupyter_output><empty_output><jupyter_text>Another way of using the Vessels endpoint is to limit searches based on vessel class. Say we are interested only in VLCC movements from Saudi Arabia:<jupyter_code># get all vessels of vlcc class
vlcc = v.Vessels().search(vessel_classes='vlcc')

# get the ids of these vessels
vlcc_ids = [ves['id'] for ves in vlcc]
# saudi arabia id
sa_search = v.Geographies().search("Saudi Arabia",exact_term_match=True)
assert len(sa_search) == 1
sa_ID = sa_search[0]['id']

# filter for vlcc movements out of saudi arabia
vlcc_query = v.CargoMovements().search(
    filter_activity="loading_state",
    filter_time_min=one_month_ago,
    filter_time_max=now,
    filter_vessels=vlcc_ids,
    filter_origins=sa_ID)
# convert to dataframe
vlcc_query.to_df(columns=required_columns).rename(relabel,axis=1)<jupyter_output><empty_output><jupyter_text>Storing IDs in a DictionaryIf there is a group of vessels of interest, it may be useful to store their IDs in a dictionary for easy access (of course, this applies to IDs for all other types of filter, as well).<jupyter_code># search for all vlcc vessels with the term 'glory'
vlcc_glory = v.Vessels().search(vessel_classes='vlcc',term='glory')
vlcc_glory_df = vlcc_glory.to_df()

# limit to those with 'glory' in the name
vlcc_glory_df = vlcc_glory_df[vlcc_glory_df['name'].str.contains('GLORY')]
vlcc_glory_df
vlcc_glory_dict = dict(zip(vlcc_glory_df['name'],vlcc_glory_df['id']))
vlcc_glory_dict
vlcc_EG_query = v.CargoMovements().search(
    filter_activity="loading_state",
    filter_time_min=one_month_ago,
    filter_time_max=now,
    filter_vessels=vlcc_glory_dict['DELTA GLORY'])
vlcc_EG_query.to_df(columns=required_columns).rename(relabel,axis=1)<jupyter_output><empty_output>
{'fraction_non_alphanumeric': 0.10514018691588785, 'fraction_numerical': 0.004380841121495327, 'mean_word_length': 6.4945295404814, 'pattern_counts': {'":': 0, '<': 13, '<?xml version=': 0, '>': 13, 'https://': 1, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '3', 'dir': 'jupyter-structured-clean-dedup', 'id': '228295', 'license': 'Apache-2.0', 'n_tokens_mistral': 1187, 'n_tokens_neox': 1154, 'n_words': 265, 'path': 'docs/examples/academy/25. Filtering by Vessel.ipynb', 'repo_name': 'VorTECHsa/python-sdk', 'size': '3424'}