Important Python Functions used in Data Science

Subhradeep Guha
4 min readJan 17, 2021

Introduction:

Function is a block of code which performs a specific task. So, if we need to perform multiples tasks in a single piece of code then we need to create multiple functions to build a complete solution. To better understand the topic, we can easily consider with our real life like- eating, running, cycling where we do multi tasks at a time. (Eg: Let’s say if I’m running my both legs are moving as well as my hands and at the same time I’m taking breathe so multiple tasks are happening at a time).

To create a function or to implement the algorithm first we define function as “def”. Also, we can define function by putting parameter inside the function.

Snap-1

To return the output from the function we need to write the “return” statement as given below:

Snap-2

There are few advantages of python functions :

a. Function reduces the code length and avoid the same codes again and again

b. Function divides the complex problem into simpler one and makes it understandable

c. Function reduces the chances of errors which makes the code more distinct.

Built-in functions used in Data Science:

There are 7 common built-in functions we mostly use in data science. Let’s understand each one by one:

  • Filter()
  • Recursion()
  • Lambda()
  • Map()
  • Reduce()
  • Enumerate()
  • Isinstance()

Let’s start.

Filter():

Filter() method constructs an iterator from elements of an iterable for which a function returns true.

Syntax of filter() method is: filter(function, iterable)

Filter method takes two parameters:

a. Function- Function that is operated on the iterable.

b. Iterable- Iterable which is to be filtered, could be sets, lists, tuples, or containers of any iterators

Snap-3

Recursion():

In Python, we know that a function can call other functions. It is even possible for the function to call itself. These types of construct are termed as recursive functions. Recursive functions make the code look clean and elegant.

Snap-4

In the above example multiply() is a recursive function as it calls itself.

Lambda():

Lambda function is a small anonymous function. A lambda function can take any number of arguments, but can only have one expression. We call it as anonymous function as it doesn’t have any name and here the function parameters and expressions come in the same line.

Snap-5

In lambda function we can multiple arguments.

Snap-6

The power of lambda is better shown when you use them as an anonymous function inside another function.

Map():

Map() function returns a map object of the results after applying the given function to each item of a given iterable (list, tuple etc.)

The returned value from map() (map object) can be passed to functions like list(), set().

Snap-7

Reduce():

The reduce() function is used to apply a particular function passed in its argument to all of the list elements mentioned in the sequence passed along.

Snap-8

We mostly use reduce function when we need any consolidated result or summation result. Here in the above pic, we have imported reduce from “functools” module and then we have executed the code.

Enumerate():

Enumerate() function keeps the count of iterations. Enumerate() method adds a counter to an iterable and returns it in a form of enumerate object. This enumerate object can then be used directly in for loops.

Snap-9

Isinstance():

The Isinstance() function returns True if the specified object is of the specified type, otherwise False.

If the type parameter is a tuple, this function will return True if the object is one of the types in the tuple.

Snap-10

Here in the above pic. “Hello” is a string value and it’s present in the type hence it has returned as True.

--

--

Subhradeep Guha

Data Scientist with a good amount analytical skill, Python, SAS, ML, Statistics skills.