Namedtuple in Python:

In Python, we have a type of container similar to dictionaries known as namedtuples() which is present in the module collection. Similar to dictionaries, they have keys that are hashed to a particular value. It supports both access from key value and iteration. This is the functionality which the dictionaries lack in python while applying it on data science.

Operations on namedtuple():

Access Operations:

  1. Access by index: In namedtuple() the attribute values are order, and these can be access using the index number. But in dictionaries, it is not accessible by index.
  2. Access by keyname: Similar to dictionaries access by keyname is allowed.
  3. using getattr(): It is another way of accessing the value by giving namedtuple and key value as argument.

Namedtuple in Python for Data Science - PST Analytics

Conversion Operations:
  1. _make(): When this function is call it returns a namedtuple() from the iterable pass as argument.
  2. _asdict(): When this function is call, it returns the Orderedict() as it is constructed from the map values of namedtuple().
  3. using “**” (double star) operator: This is a function that is there for converting a dictionary into namedtuple().

Namedtuple in Python for Data Science - PST Analytics

Additional Operations:
  1. _fields: It is a function that is there for returning all the keynames of the namespace declare.
  2. _replace(): It is a function that is there for changing the values map with pass keyname.

To learn more about it python for data science, you can check this and this as well. These blogs will help you in getting more insights of how the data science industry looks like. If you are looking for related course then it will help you in getting that as well.

Leave a Reply

Your email address will not be published. Required fields are marked *

This site uses Akismet to reduce spam. Learn how your comment data is processed.