Author name: Vikram Chiluka

Python Elias Gamma Encoding

Peter Elias created the Elias Gamma Encoding, which is used to encode a sequence of positive numbers. Let’s examine how we can use Python to encode a positive integer using this encoding technique.

What is Elias Gamma Encoding?

The Elias gamma code is a universal code for encoding a series of positive integers. It’s particularly beneficial when the upper bound of an integer can’t be determined in advance.

Assume the number to be encoded is N. The steps for Elias Gamma Encoding of N are as follows:

  • Find the greatest integer x that meets the following criteria.
  • 2x ≤ N.
  • Now, in the resulting encoding string, add x number of zeroes followed by 1. This is referred to as Unary Encoding.
  • To the result obtained in the previous step, add the x-digit binary representation of (N – 2x).
  •  The result string is the Elias Gamma Encoded.

Example

Let the Number = 19

As (19 = 2^4 + 3), the greatest feasible value of  ‘x’ in this scenario is 4.

Unary encoding:  By performing unary encoding we get 00001.

Then we need to get the four-digit binary representation of 3, which is 0011.

It should be added to 00001.

As a result, our encoded string is 000010011.

Elias Gamma Encoding in Python

Approach:

  • Import parse from urllib module using the import keyword
  • Perform the Unary encoding by using the lambda function
  • Perform the binary encoding by using the lambda function
  • Create a function say eliasEncoding() which returns the Elias Encoded string for the given number
  • Check if the number is equal to 0 using the if conditional statement
  • If it is true then return ‘0’
  • Get the base-2 log value of the given number using the log() function and
  • Store it in a variable
  • Pass the above log value to the unary_encoding() function and b, log value to the binary_encoding() function and sum up both the unary and binary encoded values
  • Return the above result
  • Pass some random number to the above-created eliasEncoding() function to perform the Elias Encoding operation and print the result.
  • The Exit of the Program.

Below is the implementation:

# Import parse from urllib module using the import keyword 
import math
# Perform the Unary encoding by using the lambda function
unary_encoding = lambda num: num * '0' + '1'
# Perform the binary encoding by using the lambda function
binary_encoding = lambda num, l = 1:("{0:0%db}" % l).format(num)
# Create a function say eliasEncoding() which returns the Elias Encoded string for the given number
def eliasEncoding(num):
    # Check if the number is equal to 0 using the if conditional statement
    if(num==0):
        # If it is true then return '0'
        return '0'
    
    # Get the base-2 log value of the given number using the log() function and 
    # Store it in a variable
    logval = int(math.log(num, 2))
 
    b = num - 2 ** logval
    # Pass the above log value to the unary_encoding() function and b, log value to the binary_encoding() function
    # and sum up both the unary and binary encoded values  
    rslt = unary_encoding(logval) + binary_encoding(b, logval)
    # Return the above result
    return rslt

# Pass some random number to the above created eliasEncoding() function 
# to perform the Elias Encoding operation and print the result
print("The Elias Encoding of the given number{10}:")
print(eliasEncoding(10))

Output:

The Elias Encoding of the given number{10}:
0001010

NOTE: It should be noted that Elias Gamma Encoding is useful when the upper bound of integers cannot be determined.

Python Elias Gamma Encoding Read More »

How to Extract a Single Value from JSON Response Using API Call in Python?

In this Python article, let us look at how to use Python to obtain single data or single values from JSON. To complete this task, we will use Python’s request module, which allows users to send HTTP requests and get responses in the form of JSON.

What is JSON?

JSON is an abbreviation for JavaScript Object Notation. It means that data is stored and transferred using a script (executable) file comprised of text in a computer language. Python has a built-in library named json that handles JSON. In order to use this feature, we must first load the json package into our Python script. The text in JSON is represented by a quoted-string, which contains the value in the key-value mapping within flower braces { }.

Example:

{
    "data": [
        {
            "ID": "1",
            "NAME": "Dhoni",
            "SALARY": "100000"
        },
        {
            "ID": "2",
            "NAME": “Virat”,
            "SALARY": "90000"
        }
     ]
}
Get the API Key from the URL:

Extracting a Single Value from JSON Response Using API Call in Python

Approach:

  • Import parse from urllib module using the import keyword
  • Import requests module using the import keyword
  • Declare the base URL using the API key variable.
  • Below is the base URL whereas ‘0f215802f0c83392e64ee40d’ is api key
  • Give some random string(here currency) as user input using the input() function and store it in a variable.
  • Give the other random string as user input using the input() function and store it in another variable.
  • Add both the currencies separated by the ‘/’ symbol to the base URL path to fetch the results.
  • Add the above path to the base URL and create the final URL.
  • Send an API request to the server and retrieve the data.
  • Get the conversion rate from the JSON data by accessing the key conversion_rate and store it in a variable
  • Print the Conversion Rate.

Below is the implementation:

# Import parse from urllib module using the import keyword 
import urllib.parse
# Import requests module using the import keyword 
import requests
# Declare the base url using the API key variable.
# Below is the base url whereas '0f215802f0c83392e64ee40d' is api key
baseUrl= "https://v6.exchangerate-api.com/v6/0f215802f0c83392e64ee40d/pair/"
# Give some random string(here currency) as user input using the input() function
# and store it in a variable.
print("Enter Some Random First Currency Value say (USD,INR) =")
firstcurrency=input()
# Give the other random string as user input using the input() function
# and store it in another variable.
print("Enter Some Random Second Currency Value say (USD,INR) =")
secondcurrency=input()
# Add both the currencies separated by '/' symbol to the base url path to fetch the results.
value = firstcurrency+"/"+secondcurrency
# Add the above path to the base url and create the final url
final_url = baseUrl+value
# Send an API request to the server and retrieve the data.
json_data = requests.get(final_url).json()
# Get the conversion rate from the json data by accessing the key conversion_rate and store it in a variable
resultValue = json_data['conversion_rate']
# Print the Conversion Rate
print("1 "+firstcurrency+" = ",resultValue,secondcurrency)
print("Conversion rate from "+firstcurrency+" to "+secondcurrency+" = ",resultValue)

Output:

Enter Some Random First Currency Value say (USD,INR) =
EUR
Enter Some Random Second Currency Value say (USD,INR) =
INR
1 EUR = 83.8568 INR
Conversion rate from EUR to INR = 83.8568

How to Extract a Single Value from JSON Response Using API Call in Python? Read More »

Python Program to Compare Two Text Files

Let us look at how to compare two text files in Python in this tutorial. We will use a variety of approaches to try to solve this problem.

Let us consider the below two text files as an example:

demotextfile_1.txt:

hello this is Python-programs 
good morning all
Then give a start to it 
welcome to the greatest python coding platform

demotextfile_2.txt:

hello this is Python-programs 
Are you good at coding? 
Then give a start to it 
good morning all

Program to Compare Two Text Files in Python

Below are the ways to compare two text files in Python:

Method #1: Using filecmp.cmp() Method

Python has a filecmp module that allows you to compare files. The filecmp.cmp() function returns a three-list of matched, errors, and mismatched files. filecmp.cmp() has two modes of operation.

shallow mode: In this mode, only file metadata such as size, modified date, and so on are compared.
deep mode: The content of the files is compared in this mode.

Approach:

  • Import filecmp module using the import keyword
  • Import os module using the import keyword
  • Give the first textfile as static input and store it in a variable.
  • Give the second textfile as static input and store it in another variable.
  • Pass the above given first and second text files as arguments to the cmp() function of the filecmp module to compare metadata in shallow mode
  • Returns True if both the textfiles are the same else False
  • Pass the above given first and second text files as arguments to the cmp() function of the filecmp module to comapre content in deep mode( shallow=False)
  • Returns True if both the textfiles are the same else False.
  • The Exit of the Program.

Below is the implementation:

# Import filecmp module using the import keyword 
import filecmp
# Import os module using the import keyword 
import os
# Give the first textfile as static input and store it in a variable.
Textfile_1 = "demotextfile_1.txt"
# Give the second textfile as static input and store it in another variable.
Textfile_2 = "demotextfile_2.txt"

# Pass the above given first and second text files as arguments to the cmp() function 
# of the filecmp module to compare metadata in shallow mode
# Returns True if both the textfiles are same else False
rslt = filecmp.cmp(Textfile_1, Textfile_2)
print(rslt)

# Pass the above given first and second text files as arguments to the cmp() function 
# of the filecmp module to comapre content in deep mode( shallow=False)
# Returns True if both the textfiles are same else False
rslt = filecmp.cmp(Textfile_1, Textfile_2, shallow=False)
print(rslt)

Output:

False
False

Method #2: Using line by line Comparision

Approach:

  • Open the first text file in read-only mode using the open() function by passing filename/path, mode as arguments to it and store it in a variable.
  • Open the second text file in read-only mode using the open() function by passing filename/path, mode as arguments to it and store it in another variable.
  • Take a variable and initialize its value with 0 which represents the line number.
  • Loop in each line of the given first text file using the for loop.
  • Increment the above initialized variable value by 1 which holds the line number.
  • Loop in each line of the given second text file using the other nested for loop.
  • Check if each line of the first text file is equal/similar to the corresponding line of the second text file using the if conditional statement.
  • If it is true, then print the respective line number.
  • Print Both the lines of given two text file are the same.
  • Else print the respective line number and corresponding lines of both the text files.
  • Close the given first text file using the close() function.
  • Close the given second text file using the close() function.
  • The Exit of the Program.

Below is the implementation:

# Open the first text file in read-only mode using the open() function
# by passing filename/path, mode as arguments to it and store it in a variable.
Textfile_1 = open("demotextfile_1.txt", "r")
# Open the second text file in read-only mode using the open() function
# by passing filename/path, mode as arguments to it and store it in another variable.
Textfile_2 = open("demotextfile_2.txt", "r")
# Take a variable and initialize its value with 0 which represents the line number
i = 0
# Loop in each line of the given first text file using the for loop
for f1_line in Textfile_1:
    # Increment the above initialized variable value by 1 which holds the line number
    i += 1
    # Loop in each line of the given second text file using the other nested for loop
    for f2_line in Textfile_2:
      # Check if each line of the first text file is equal/similar to the corresponding line 
      # of the second text file using the if conditional statement
      if f1_line == f2_line:
          # If it is true, then print the respective line number 
          print("line_",i,": ")
          # Print Both the lines of given two text file are the same
          print("Both the lines of given two text file are the same")
          print()
      else:
          # Else print the respective line number and corresponding lines of 
          # both the text files
          print("line_",i,": ")
          print("Textfile_1: ", f1_line)
          print("Textfile_2: ", f2_line)
      break

# Close the given first text file using the close() function
Textfile_1.close()
# Close the given second text file using the close() function
Textfile_2.close()

Output:

line_ 1 : 
Both the lines of given two text file are the same

line_ 2 : 
Textfile_1: good morning all

Textfile_2: Are you good at coding?

line_ 3 : 
Both the lines of given two text file are the same

line_ 4 : 
Textfile_1: welcome to the greatest python coding platform

Textfile_2: good morning all

Python Program to Compare Two Text Files Read More »

Python Program to Store a Sparse Matrix as a Dictionary

In this article, we will learn how to use a dictionary in Python to store the sparse matrix in an efficient manner. We frequently encounter instances in which Memory is wasted for improper/inefficient data storage. To solve this problem, we can use data structures like the dictionary in Python.

Dictionary in Python:

A dictionary is a type of data structure that stores values as a pair of key and value.

A colon (:) separates each of its keys from its value.
Commas (,) are used to separate consecutive items.

Syntax:

dict  =  {key_1: value1, key_2: value2...}

What is Sparse Matrix?

It is a matrix with very few non-zero elements. The majority of its elements are zero. We waste a lot of memory space when we express it with a 2-dimensional array.

Because the majority of its elements are zero, we aim to store only the non-zero elements because the remainder of the elements will be zero anyhow. So the question is, why employ this sparse matrix?

The explanation is that these matrices are extremely beneficial for storing data with a large number of zero-valued elements because they save a lot of memory while simultaneously speeding up processing.

Example

0 0 0 1 0 
6 0 0 0 8
0 2 0 0 0

// sparse matrix with 4 non-zero elements

The usage of a 2D array to represent a sparse matrix wastes a lot of memory because the zeroes in the matrix are useless in most circumstances. As a result, rather than keeping zeroes alongside non-zero elements, we just store non-zero elements.

These efficient methods simply require the only non-zero values to be saved together with their index, allowing the original matrix to be accessed when needed. The use of a dictionary is one such efficient method in Python. In Python, dictionaries, like maps in Java, store data in key-value pairs. The data in a dictionary is stored in an unordered way.

Program to Store a Sparse Matrix as a Dictionary in Python

Approach:

  • Give the matrix as static input and store it in a variable
  • Take a new empty dictionary and store it in another variable.
  • Loop till the length of the given matrix(rows) using the for loop
  • Loop till the number of columns using another Nested For loop
  • Print the matrix Value
  • Check if the matrix values is equal to 0 or not using the If conditional statement
  • If the value is non zero then
  • Take a tuple(i,j)(where i is the row number and j is the column number) as a key to the dictionary and value as matrix value
  • Printing new line.
  • The Exit of the Program.

Below is the implementation:

# Give the matrix as static input and store it in a variable
mat=[[0,5,0,0,0],
     [2,0,0,0,0],
     [0,0,0,3,0]]
# Take a new empty dictionary and store it in another variable.
dic={}
print("Printing the Sparse Matrix")
# Loop till the length of the given matrix(rows) using the for loop
for i in range (len(mat)):   
     # Loop till the number of columns using another Nested For loop
     for j in range(len(mat[i])):
         # Print the matrix Value
         print(mat[i][j],end=' ')
         # Check if the matrix valies is equal to 0 or not using the If conditional statement
         if mat[i][j]!=0:
           # If the value is non zero then
           # Take a tuple(i,j)(where i is the row number and j is the column number) as a key 
           # to the dictionary and value as matrix value
            dic[(i,j)]=mat[i][j]
     # Printing new line
     print("\n")
print("\nPrinting the Sparse Matrix efficiently which is represented as Dictionary :")
print(dic)

Output:

Printing the Sparse Matrix
0 5 0 0 0

2 0 0 0 0

0 0 0 3 0


Printing the Sparse Matrix efficiently which is represented as Dictionary :
{(0, 1): 5, (1, 0): 2, (2, 3): 3}

Python Program to Store a Sparse Matrix as a Dictionary Read More »

Python Program to Show Scatter in Terms of Tuple

Python functions that accept variable-length parameters. In many scenarios, depending on the needs, we will have to deal with a variable number of arguments.

Variable-length Arguments

It is a feature that enables the function to accept any number of parameters. Many built-in Python functions, such as max (), min (), sum (), and so on, take variable-length arguments.

These functions can accept an unlimited number of parameters. In such circumstances, we use the symbol ‘*’ to indicate that the argument is of variable length.

Any parameter that begins with the ‘*’ symbol is known as gather and signifies a variable-length argument.
The antonym(opposite) of gather is scatter.
So, if there is a function that accepts several inputs but not a tuple, the tuple is scattered and passed to individual elements.

Program to Show Scatter in Terms of Tuple in Python

Approach:

  • Give the tuple with with 2 elements a static input and store it in a variable
  • Here, the divmod() function doesn’t accept a tuple.
  • So, the tuple values are scattered and passed using the ‘*’ symbol.
  • Here divmod() function returns quotient and remainder and are stored in two separate variables
  • Print the above-obtained quotient and remainder values.
  • The Exit of the Program.

Below is the implementation:

# Give the tuple with with 2 elements a static input and store it in a variable
gvn_tuple = (25, 2)
# Here, the divmod() function doesn't accept a tuple.
# so, the tuple values are scattered and passed using the '*' symbol.
# Here divmod() function returns quotient and remainder and are
# stored in two separate variables
quotient, remaindr = divmod(*gvn_tuple)
# Print the above obtained quotient and remainder values
print("quotient:", quotient," ", "remainder:", remaindr)

Output:

quotient: 12 remainder: 1

Explanation:

The tuple was sent as a single argument in the code provided, but the divmod() function needs two inputs. As a result, the sign ‘*’ indicates that the argument may contain more than one argument. (In this case, it is quotient and remainder.)

The example given here is a division operation. This approach can be used to a variety of Python functions.

The function extracts and scatters them, then performs the relevant procedure. After obtaining the output, it is scattered and shown.

Python Program to Show Scatter in Terms of Tuple Read More »

Methods for Removing a Column from a Python Dataframe

Python provides us with a number of data structures through which we can interact with data and perform operations on it. Particularly when it comes to data science and analysis, the data structures provided by Python have given shape to the processing it.

Dataframe:

Python provides one such data structure, DataFrame. It saves data in the form of rows and columns. The datasets can be analyzed within the environment. These synchronized rows and columns are ready for data preprocessing and manipulation.

The Python Pandas module provides a data structure called a DataFrame. It organizes data into rows and columns and stores it. As a result, we can have the data in the form of a matrix, with the entities represented as rows and columns.

Removing a Column from a Python Dataframe

There are 3 simple methods to do this task.They are:

  1. Using pop() method
  2. Using del Keyword
  3. Using drop() method
1)Using pop() method

The pandas.dataframe.pop() method is used to remove or delete a column from a data frame by simply passing the column name as an argument.

Syntax:

pandas.dataframe.pop('ColumnName')

For Example:

Approach:

  • Import pandas module using the import keyword.
  • Give some random list of data(as dictionary) and store it in a variable.
  • Pass the given data to the DataFrame() function and store it in another variable.
  • Print the above result.
  • Remove some random column(salary) from the given dataframe using the pop() method by passing the column name as an argument.
  • Print the above data after removing the specified column(salary).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword
import pandas as pd
# Give some random list of data and store it in a variable
gvn_data = {"ID": [11, 12, 13, 14, 15, 16], "Name": ["peter", "irfan", "mary",
                                                     "riya", "virat", "sunny"], "salary": [10000, 25000, 15000, 50000, 30000, 22000]}
# Pass the given data to the DataFrame() function and store it in another variable
block_data = pd.DataFrame(gvn_data)
# Print the above result
print("The given input Dataframe: ")
print(block_data)
print()
# Remove some random column(salary) from the given dataframe using the pop() method
# by passing the columnname as an argument.
block_data.pop('salary')
# Print the above data after removing the specified column(salary)
print("The given data after removing the 'salary' column: ")
print(block_data)

Output:

The given input Dataframe: 
   ID   Name  salary
0  11  peter   10000
1  12  irfan   25000
2  13   mary   15000
3  14   riya   50000
4  15  virat   30000
5  16  sunny   22000

The given data after removing the 'salary' column: 
   ID   Name
0  11  peter
1  12  irfan
2  13   mary
3  14   riya
4  15  virat
5  16  sunny
2)Using del Keyword

The Python del keyword can also be used to remove a column from a data frame. In Python, the del keyword is generally used to delete or flush out objects.

Syntax:

del dataframe['ColumnName']

Approach:

  • Import pandas module using the import keyword.
  • Give some random list of data(as dictionary) and store it in a variable.
  • Pass the given data to the DataFrame() function and store it in another variable.
  • Print the above result.
  • Delete some random column(Name) from the given dataframe using the del keyword.
  • Print the above data after removing the specified column(Name).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword
import pandas as pd
# Give some random list of data and store it in a variable
gvn_data = {"ID": [11, 12, 13, 14, 15, 16], "Name": ["peter", "irfan", "mary",
                                                     "riya", "virat", "sunny"], "salary": [10000, 25000, 15000, 50000, 30000, 22000]}
# Pass the given data to the DataFrame() function and store it in another variable
block_data = pd.DataFrame(gvn_data)
# Print the above result
print("The given input Dataframe: ")
print(block_data)
print()
# Delete some random column(Name) from the given dataframe using the del keyword
del block_data['Name']
# Print the above data after removing the specified column(Name)
print("The given data after removing the 'Name' column: ")
print(block_data)

Output:

The given input Dataframe: 
   ID   Name  salary
0  11  peter   10000
1  12  irfan   25000
2  13   mary   15000
3  14   riya   50000
4  15  virat   30000
5  16  sunny   22000

The given data after removing the 'Name' column: 
   ID  salary
0  11   10000
1  12   25000
2  13   15000
3  14   50000
4  15   30000
5  16   22000
3)Using drop() Method

We can remove values from a data frame using the pandas.dataframe.drop() function. The values can be either row- or column-oriented.

Syntax:

dataframe.drop('ColumnName', inplace=True, axis=1)

ColumnName: The column that you want to remove.

inplace: If set inplace=True, the changes are stored in a new object that is created without affecting the original dataframe.
axis: axis= 1 represents column-wise operations and 0 represents row-wise operations.

For Example:

Approach:

  • Import pandas module using the import keyword.
  • Give some random list of data(as dictionary) and store it in a variable.
  • Pass the given data to the DataFrame() function and store it in another variable.
  • Print the above result.
  • Remove some random column(salary) from the given dataframe using the drop() method by passing the column-name, inplace=True, axis=1 as the arguments to it.
  • Print the above data after removing the specified column(salary).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword
import pandas as pd
# Give some random list of data(as dictionary) and store it in a variable
gvn_data = {"ID": [11, 12, 13, 14, 15, 16], "Name": ["peter", "irfan", "mary",
                                                     "riya", "virat", "sunny"], "salary": [10000, 25000, 15000, 50000, 30000, 22000]}
# Pass the given data to the DataFrame() function and store it in another variable
block_data = pd.DataFrame(gvn_data)
# Print the above result
print("The given input Dataframe: ")
print(block_data)
print()
# Remove some random column(salary) from the given dataframe using the drop() method
# by passing the columnname, inplace=True, axis=1 as the arguments to it.
block_data.drop('salary', inplace=True, axis=1)
# Print the above data after removing the specified column(salary)
print("The given data after removing the 'salary' column: ")
print(block_data)

Output:

The given input Dataframe: 
   ID   Name  salary
0  11  peter   10000
1  12  irfan   25000
2  13   mary   15000
3  14   riya   50000
4  15  virat   30000
5  16  sunny   22000

The given data after removing the 'salary' column: 
   ID   Name
0  11  peter
1  12  irfan
2  13   mary
3  14   riya
4  15  virat
5  16  sunny

 

 

Methods for Removing a Column from a Python Dataframe Read More »

Indexing in Python with Examples

What exactly is indexing in Python? – As simple as it appears, explaining how indexing works in Python can be a little tricky. So take a seat and read this article to gain a better understanding of indexing in Python.

Iterables:

Before we get started with indexing, let’s define iterables and their primary function. Iterable knowledge is essential for getting behind indexing.

So, what exactly are iterables?

In Python, it is a special type of object that may be iterated over. That is, you can traverse through all of the various elements or entities present within the object or entity. It is simple to accomplish using for loops.

All iterable items have two special methods called __iter__() or __getitem__() that implement Sequence Semantics.

Lists, tuples, strings etc are examples of iterables in python.

Example

1)For Lists

Approach:

  • Give the list as static input and store it in a variable.
  • The list is iterable.
  • Iterate in the above list using the for loop.
  • Inside the for loop, print the elements of the given list by printing the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
# List is an iterable
gvn_lst = [1, 2, 3, 4, 5]
# Iterate in the above list using the for loop.
for elemnt in gvn_lst:
    # Inside the for loop, print the element of the given list by printing the
    # iterator value.
    print(elemnt)

Output:

1
2
3
4
5

2)For Strings

Example

Approach:

  • Give the string as static input and store it in a variable.
  • Strings are also examples of an iterable.
  • Iterate in the above string using the for loop.
  • Inside the for loop, print the characters of the given string by printing the iterator value.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
# Strings are also examples of an iterable.
gvn_str = "Python-programs"
# Iterate in the above string using the for loop.
for chrctr in gvn_str:
    # Inside the for loop, print the characters of the given string by printing the
    # iterator value.
    print(chrctr)

Output:

P
y
t
h
o
n
-
p
r
o
g
r
a
m
s

Now that we know what Iterables are in Python. How does this relate to indexing?

Indexing in Python

Indexing in Python is a method of referring the individual items within an iterable based on their position.

In other words, you can directly access the desired elements within an iterable and perform various operations based on your requirements.

Note: 

Objects in Python are “zero-indexed,” which means the position count starts at zero. A similar pattern is followed by several other programming languages. For example C, C++, java, etc,
So, let’s say there are 6 elements in a list. The first element (i.e. the leftmost element) then takes the “zeroth” position, followed by the elements in the first, second, third, fourth, and fifth positions.

Let  array =     10  11  12  13  14

indexes :          0    1    2   3    4

Example

Approach:

  • Give the list as static input and store it in a variable.
  • Apply index() function to the given list by passing “Python” as an argument to it to get index(position) of “Python”.
  • Store it in another variable.
  • Print the index of “Python”.
  • Similarly, print the index of “welcome”.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["welcome", "to", "Python", "programs"]
# Apply index() function to the given list by passing "Python" as an argument
# to it to get index(position) of "Python"
# Store it in a variable.
indx = gvn_lst.index("Python")
# Print the index of "Python".
print("The Index of Python = ", indx)
# Similarly, print the index of "welcome".
print("The Index of welcome = ", gvn_lst.index("welcome"))

Output:

The Index of Python =  2
The Index of welcome =  0

When the index() method on a list is invoked with the item name as an argument, the index of a specific item within the list is displayed.

Now, we will see how to use the index() method on iterable objects.

Index Operator in Python

The Python Index Operator is represented by opening and closing square brackets [ ].

Syntax:

objectname[n]

n – It is an Integer. It is representing the position of the element we want to access.

Example

Approach:

  • Give the string as static input and store it in a variable.
  • Pass some random number to the given string to get the element present at the given index number.
  • Store it in another variable.
  • Print the element present at the 4th index.
  • Similarly, try for the other numbers.
  • The Exit of the Program.

Below is the implementation:

# Give the string as static input and store it in a variable.
gvn_str = "Python-programs"
# Pass some random number to the given string to get the element present
# at the given index number.
# Store it in another variable.
elemnt = gvn_str[4]
# Print the element present at the 4th index.
print(elemnt)
# Similarly, try for the other numbers.
print(gvn_str[6])
print(gvn_str[2])
print(gvn_str[8])

Output:

o
-
t
r

Negative Indexing

We just learnt how to use indexing in Lists and Strings to find specific objects of interest. Although we’ve used a positive integer inside our index operator (the square brackets) in all of our prior examples, this isn’t always necessary.

Negative integers are useful when we want to find the last few items of a list or when we want to index the list from the opposite end. Negative Indexing refers to the process of indexing from the opposite end.

Note: It should be noted that with negative indexing, the last element is represented by -1 rather than -0.

# Give the list as static input and store it in a variable.
gvn_lst = ["welcome", "to", "Python", "programs"]
# Using negative indexing to print the last item in a given list
print(gvn_lst[-1])
# Printing the first item in a given list
print(gvn_lst[-4])
# Printing the last second item in a given list
print(gvn_lst[-3])

Output:

programs
welcome
to

Indexing in Python with Examples Read More »

In Python, what does “Immutable” mean?

In Python, when every entity is an object, what does immutable mean? Python, unlike some other programming languages, does not require you to explicitly specify the type of data you’re assigning to a variable. Instead, it assigns the data type automatically based on the value you specify.

In summary, each variable contains an object instance and is assigned a unique object ID generated during program execution. The Object ID is an integer that represents the memory location where the variable’s value is stored.

To obtain the ID of each object, open Python Shell and invoke the default id() function, passing the variable name as a parameter.

Example:

# Give the string as static input and store it in a variable.
# (variable Initialization)
gvn_str = "welcome to Pyton-programs"
# Invoke the id() function, by passing the variable name(given string) as
# an argument.
# Store it in a variable.
rslt_id = id(gvn_str)
# Print the given string's Object id.
print("The given string's object Id = ", rslt_id)

Output:

The given string's Object id =  139677158260784

Mutable and Immutable Objects

In Python, each variable holds an instance of an object. In Python, there are two types of objects: mutable objects and immutable objects. When an object is created, it is given a unique object id. The type of the object is determined at runtime and cannot be modified later. Conversely, if it is a mutable object, its state can be modified.

To summarise the differences, mutable objects can change their state or content, but immutable objects cannot.

Immutable Objects: These are built-in types such as int, float, boolean, string, unicode, and tuple. In other words, an immutable object cannot be modified once it has been created.

For Tuples:

Example

# Give the string as static input and store it in a variable.
gvn_tupl = (5, 3, 1, 8)
# Print the tuple before modification.
print(gvn_tupl)
# Modidify the element present at the zeroth index to 10
gvn_tupl[0] = 10
# Print the tuple after modification.
print(gvn_tupl)

Output:

Traceback (most recent call last):
  File "/home/3ca1ca5cd18ba9417154eab3aa732be9.py", line 6, in <module>
    gvn_tupl[0] = 10
TypeError: 'tuple' object does not support item assignment

Explanation:

Here we get an error beacuase, tuple is immutable. That means we cannot 
change the values of it once it is created.

For Integers

Approach:

  • Give the number as static input and store it in a variable.
  • Get the ID of the given number by passing it as an argument to the id() function.
  • Store it in another variable.
  • Modify/change the given number with some other random number and store it in the same variable.
  • Get the ID of the modified number by passing it as an argument to the id() function.
  • Store it in another variable.
  • Check if the ID if the given number is the same before and after modification using the if conditional statement.
  • If it is True, then print “The Id if the given number is same before and after modification.
  • Else print “The Id if the given number is NOT same before and after modification”.
  • The Exit of the Program.

Below is the implementation:

# Give the number as static input and store it in a variable.
gvn_numb = 50
# Get the ID of the given number by passing it as an argument to the id() function.
# Store it in another variable.
orignl_id = id(gvn_numb)
# Modify/change the given number with some other random number and
# store it in the same variable.
gvn_numb = 100
# Get the ID of the modified number by passing it as an argument to the
# id() function.
# Store it in another variable.
chngd_id = id(gvn_numb)
# Check if the ID if the given number is the same before and after modification
# using the if conditional statement.
if orignl_id == chngd_id:
    # If it is True, then print "The Id of the given number is same before
        # and after modification.
    print("The Id of the given number is same before and after modification")
else:
    # Else print "The Id of the given number is NOT same before and after modification".
    print("The Id of the given number is NOT same before and after modification")

Output:

The Id of the given number is NOT same before and after modification

Mutable Objects:

Mutable objects can be modified or changed even after they are created. we can change the values of it once it is created.

Examples of mutable objects in python are List, Dictionaries, and Sets. Custom classes are mutable in general.

The memory location or list ID stayed constant although the values changed. That is, Python allocated more memory space to the location in order to consider the new values.

This means that a list is a “mutable” or “changeable” object.

Approach:

  • Give the list as static input and store it in a variable.
  • Print the given list before modification.
  • Modify the element present at the zeroth index of the given list to “hello”.
  • Print the given list after modification.
  • The Exit of the Program.

Below is the implementation:

# Give the list as static input and store it in a variable.
gvn_lst = ["welcome", "to", "python", "programs"]
# Print the given list before modification.
print("The given list before Modification: ")
print(gvn_lst)
print()
# Modify the element present at the zeroth index of given list to "hello"
gvn_lst[0] = "hello"
# Print the given list after modification.
print("The given list after Modification: ")
print(gvn_lst)

Output:

The given list before Modification: 
['welcome', 'to', 'python', 'programs']

The given list after Modification: 
['hello', 'to', 'python', 'programs']

Brief Recap

Python handles mutable and immutable objects differently. Immutable objects are easier to access and more expensive to change because they necessitate the generation of a duplicate.
Mutable things, on the other hand, are easily changed.
When it is necessary to change the size or content of an object, it is best to use changeable objects.

Exception: However, there is an exception in the case of immutability. In Python, we know that a tuple is immutable. However, the tuple is made up of a sequence of names with fixed bindings to objects.

Let us consider a tuple

 gvn_tup = ([3, 4, 5], 'names')

The tuple is made up of a string and a list. Because strings are immutable, we cannot change their value. However, the list’s contents are subject to change. The tuple itself is not mutable, but it contains mutable objects.

As a general rule, Generally, Primitive-like types are almost certainly immutable, whereas Customized Container-like types are typically mutable.

Immutable Objects have the advantage of being significantly faster to access than the mutable counterparts.

 

In Python, what does “Immutable” mean? Read More »

In Python, How do you get Unique Values from a Dataframe?

Pandas DataFrames really amazing. DataFrames in Python makes data manipulation very user-friendly.

Pandas allow you to import large datasets and then manipulate them effectively. CSV data can be easily imported into a Pandas DataFrame.

What are Python Dataframes?

Dataframes are two-dimensional labeled data structures with columns of various types.
DataFrames can be used for a wide range of analyses.

Often, the dataset is too large, and it is impossible to examine the entire dataset at once. Instead, we’d like to see the Dataframe’s summary.
We can get the first five rows of the dataset as well as a quick statistical summary of the data. Aside from that, we can gain information about the types of columns in our dataset.

DataFrame is a data structure offered by the Pandas module to cope with large datasets with several dimensions, such as large csv or excel files.

Because we may store a huge volume of data in a data frame, we frequently encounter situations where we need to find the unique data values from a dataset that may contain redundant or repeated values.

This is where the pandas.dataframe.unique() function comes in.

pandas.unique() Function in Python

The pandas.unique() function returns the dataset’s unique values.

It basically employs a hash table-based technique to return the non-redundant values from the set of values existing in the data frame/series data structure.

For Example:

Let dataset values = 5, 6, 7, 5, 2, 6

The output we get by applying unique function = 5, 6, 7,2

We were able to readily find the dataset’s unique values this way.

Syntax:

pandas.unique(data)

When dealing with 1-Dimensional data, the above syntax comes in handy. It symbolizes or represents the unique value among the 1-Dimensional data values (Series data structure).

But what if the data has more than one dimension, such as rows and columns? Yes, we have a solution for it in the syntax below–

Syntax For Multidimensional data:

pandas.dataframe.column-name.unique()

The above syntax allows us to extract unique values from a specific column of a dataset.

It is preferable for the data to be of the categorical type in order for the unique function to produce accurate results. Furthermore, the data is displayed in the order in which it appears in the dataset.

unique() function with Pandas Series

Example

Approach:

  • Import pandas module using the import keyword.
  • Give the list as static input and store it in a variable.
  • Pass the given list as an argument to the pandas.Series() function and store it in another variable.
  • Since the list has only one dimension, we turned it into a series data structure.
  • Pass the above data as an argument to the pandas.unique() function to get all the unique values from the given list(data).
  • Store it in another variable.
  • Print all Unique elements from the given list.
  • The Exit of the Program.

Below is the implementation:

# Import pandas module using the import keyword
import pandas
# Give the list as static input and store it in a variable.
gvn_lst = [5, 6, 7, 5, 2, 6]
# Pass the given list as an argument to the pandas.Series() function and
# store it in another variable.
# Since the list has only one dimension, we turned it to a series data structure.
data_frme = pandas.Series(gvn_lst)
# Pass the above data as an argument to the pandas.unique() function to
# get all the unique values from the given list(data).
# Store it in another variable
uniqval_lst = pandas.unique(data_frme)
# Print all Unique elements from the given list
print("The all Unique elements from the given list = ")
print(uniqval_lst)

Output:

The all Unique elements from the given list = 
[5 6 7 2]
unique() function with Pandas DataFrame

Import the dataset first as shown below:

Importing the Dataset:

Import the dataset into a Pandas Dataframe.

Approach:

  • Import pandas module as pd using the import keyword.
  • Import dataset using read_csv() function by passing the dataset name as an argument to it.
  • Store it in a variable.
  • Print the above dataset if you want to see the dataset(here we just imported).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by passing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')

This will save the dataset in the variable ‘cereal_dataset ‘ as a DataFrame.

pandas.dataframe.nunique() function:

The unique values present in each column of the dataframe are represented by the pandas.dataframe.nunique() function.

Apply nunique() function to the given dataset to get all the unique values present in each column of the dataframe.

cereal_dataset.nunique()

Example:

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by pasing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')
# Apply nunique() function to the given dataset to get all the unique
# values present in each column of the dataframe.
cereal_dataset.nunique()

Output:

name        77
mfr          7
type         2
calories    11
protein      6
fat          5
sodium      27
fiber       13
carbo       22
sugars      17
potass      36
vitamins     3
shelf        3
weight       7
cups        12
rating      77
dtype: int64

The below is the code to represent the unique values in the column ‘vitamins’.

cereal_dataset.vitamins.unique()

Example

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by pasing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')
# Apply unique() function to the vitamins column in the given dataset to 
# get all the unique values in the column 'vitamins'.
cereal_dataset.vitamins.unique()

Output:

array([ 25, 0, 100])

 

In Python, How do you get Unique Values from a Dataframe? Read More »

Pandas Math Functions for Data Analysis

Python is a superb language for data analysis, owing to its fantastic ecosystem of data-centric Python tools. Pandas is one of these packages, and it greatly simplifies data import and analysis.
There are several essential math operations that can be done on a pandas series to ease data analysis in Python and save a significant amount of time.

Data analysis is basically the extraction of meaningful information from a raw data source. This information provides us with an idea of how the data is distributed and structured.

Let us go through the following Pandas math functions:

  • mean() function
  • sum() function
  • median() function
  • min() and max() functions
  • value_counts() function
  • describe() function

Here we used the cereal dataset as an example.

Before going to analyze the following Pandas math functions, first import the dataset.

Importing the Dataset

Import the dataset into a Pandas Dataframe.

Approach:

  • Import pandas module as pd using the import keyword.
  • Import dataset using read_csv() function by passing the dataset name as an argument to it.
  • Store it in a variable.
  • Print the above dataset if you want to see the dataset(here we just imported).
  • The Exit of the Program.

Below is the implementation:

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by passing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')

This will save the dataset in the variable ‘cereal_dataset ‘ as a DataFrame.

1)mean() function in Pandas

Mean is a statistical value that represents the whole distribution of data in a single number/value.

We can acquire the mean value for a single column or many columns, i.e. the complete dataset, by using the dataframe.mean() function.

Apply the mean() function to the dataset to get the mean of all the columns in a dataset.

cereal_dataset.mean()

Example

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by pasing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')
# Apply the mean() function to the above dataset to get the mean of all the 
# columns in a dataset.
cereal_dataset.mean()

Output:

calories    106.883117
protein       2.545455
fat           1.012987
sodium      159.675325
fiber         2.151948
carbo        14.597403
sugars        6.922078
potass       96.077922
vitamins     28.246753
shelf         2.207792
weight        1.029610
cups          0.821039
rating       42.665705
dtype: float64

2)sum() function in Pandas

In addition to the mean() function, we can utilize the Pandas sum() function to get the sum of the values of the columns on a bigger scale. This allows us to have a more quantitative view of the data.

Apply the sum() function to the dataset to calculate the sum of each column in the entire dataset.

cereal_dataset.sum()

Example

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by pasing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')
# Apply the sum() function to the above dataset to calculate the 
# sum of each column in the entire dataset.
cereal_dataset.sum()

Output:

name        100% Bran100% Natural BranAll-BranAll-Bran wit...
mfr         NQKKRGKGRPQGGGGRKKGKNKGRKKKPKPPGPPPQGPKKGQGARR...
type        CCCCCCCCCCCCCCCCCCCCHCCCCCCCCCCCCCCCCCCCCCCHCC...
calories                                                 8230
protein                                                   196
fat                                                        78
sodium                                                  12295
fiber                                                   165.7
carbo                                                    1124
sugars                                                    533
potass                                                   7398
vitamins                                                 2175
shelf                                                     170
weight                                                  79.28
cups                                                    63.22
rating                                                3285.26
dtype: object

3)median() function in Pandas

The median() function returns the 50 percentile or central value of a set of data (dataset).

Apply the median() function on the dataset to get the 50 percentile or central value of all columns of the dataset.

cereal_dataset.median()

Example

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by pasing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')
# Apply the median() function on the dataset to get the 50 percentile or 
# central value of all columns of the dataset.
cereal_dataset.median()

Output:

calories    110.000000
protein       3.000000
fat           1.000000
sodium      180.000000
fiber         2.000000
carbo        14.000000
sugars        7.000000
potass       90.000000
vitamins     25.000000
shelf         2.000000
weight        1.000000
cups          0.750000
rating       40.400208
dtype: float64

4)min() and max() functions in Pandas

We can acquire the minimum and maximum values of each column of the dataset as well as a single column of the dataframe using the min() and max() functions.

Apply the max() function on the dataset to get the maximum limit of each column in the dataset.

cereal_dataset.max()

similarly, do the same for the min() function to get the minimum values of each column of the dataset.

cereal_dataset.min()

Example

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by pasing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')
# Apply the max() function on the dataset to get the maximum values
# of each column in the dataset.
print("The maximum values of each column in the dataset:")
cereal_dataset.max()

Output:

The maximum values of each column in the dataset:
name        Wheaties Honey Gold
mfr                           R
type                          H
calories                    160
protein                       6
fat                           5
sodium                      320
fiber                        14
carbo                        23
sugars                       15
potass                      330
vitamins                    100
shelf                         3
weight                      1.5
cups                        1.5
rating                  93.7049
dtype: object

5)value_counts() function in Pandas

We get the count of each category or group in a variable using the value_counts() function. It is useful when dealing with categorical variables.

Apply the value_counts() function on the dataset vitamins variable to obtain the count of each group in the variable as a separate category.

cereal_dataset.vitamins.value_counts()

Example

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by pasing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')
# Apply the value_counts() function on the dataset vitamins variable to obtain the 
# count of each group in the variable as a separate category.
cereal_dataset.vitamins.value_counts()

Output:

25     63
0       8
100     6
Name: vitamins, dtype: int64

5)describe() function in Pandas

We obtain the statistical information of the given dataset all at once using the describe() function.

Apply the describe() function to the dataset to obtain the statistical information of the given dataset all at once

cereal_dataset.describe()

Example

# Import pandas module as pd using the import keyword
import pandas as pd
# Import dataset using read_csv() function by pasing the dataset name as
# an argument to it.
# Store it in a variable.
cereal_dataset = pd.read_csv('cereal.csv')
# Apply the describe() function to the dataset to obtain the statistical
# information of the given dataset all at once 
cereal_dataset.describe()

Output:

caloriesproteinfatsodiumfibercarbosugarspotassvitaminsshelfweightcupsrating
count77.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.00000077.000000
mean106.8831172.5454551.012987159.6753252.15194814.5974036.92207896.07792228.2467532.2077921.0296100.82103942.665705
std19.4841191.0947901.00647383.8322952.3833644.2789564.44488571.28681322.3425230.8325240.1504770.23271614.047289
min50.0000001.0000000.0000000.0000000.000000-1.000000-1.000000-1.0000000.0000001.0000000.5000000.25000018.042851
25%100.0000002.0000000.000000130.0000001.00000012.0000003.00000040.00000025.0000001.0000001.0000000.67000033.174094
50%110.0000003.0000001.000000180.0000002.00000014.0000007.00000090.00000025.0000002.0000001.0000000.75000040.400208
75%110.0000003.0000002.000000210.0000003.00000017.00000011.000000120.00000025.0000003.0000001.0000001.00000050.828392
max160.0000006.0000005.000000320.00000014.00000023.00000015.000000330.000000100.0000003.0000001.5000001.50000093.704912

 

Pandas Math Functions for Data Analysis Read More »