Python

Pandas- Find maximum values & position in columns or rows of a Dataframe

Pandas: Find maximum values & position in columns or rows of a Dataframe | How to find the max value of a pandas DataFrame column in Python?

In this article, we will discuss how to find maximum value & position in rows or columns of a Dataframe and its index position.

DataFrame.max()

Python pandas provide a member function in the dataframe to find the maximum value.

Syntax:

DataFrame.max(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

Dataframe.max() accepts these arguments:

axis: Where max element will be searched

skipna: Default is True means if not provided it will be skipped.

Let’s create a dataframe,

import pandas as pd
import numpy as np
# List of Tuples
matrix = [(17, 15, 12),
          (53, np.NaN, 10),
          (46, 34, 11),
          (35, 45, np.NaN),
          (76, 26, 13)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))
print(dfObj)

Output:

   x             y      z
a 17     15.0   12.0
b 53     NaN   10.0
c 46      34.0   11.0
d 35      45.0   NaN
e 76      26.0   13.0

Get maximum values in every row & column of the Dataframe

Here, you will find two ways to get the maximum values in dataframe

Also Check: 

Get maximum values of every column

In this, we will call the max() function to find the maximum value of every column in DataFrame.

import pandas as pd
import numpy as np
# List of Tuples
matrix = [(17, 15, 12),
          (53, np.NaN, 10),
          (46, 34, 11),
          (35, 45, np.NaN),
          (76, 26, 13)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))
# Get a series containing maximum value of each column
maxValuesObj = dfObj.max()
print('Maximum value in each column : ')
print(maxValuesObj)

Output:

Maximum value in each column :
x 76.0
y 45.0
z 13.0

Get maximum values of every row

In this also we will call the max() function to find the maximum value of every row in DataFrame.

import pandas as pd
import numpy as np
# List of Tuples
matrix = [(17, 15, 12),
          (53, np.NaN, 10),
          (46, 34, 11),
          (35, 45, np.NaN),
          (76, 26, 13)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))
# Get a series containing maximum value of each row
maxValuesObj = dfObj.max(axis=1)
print('Maximum value in each row : ')
print(maxValuesObj)

Output:

Maximum value in each row :
a   17.0
b   53.0
c   46.0
d   45.0
e   76.0

So in the above example, you can see that it returned a series with a row index label and maximum value of each row.

Get maximum values of every column without skipping NaN

import pandas as pd
import numpy as np
# List of Tuples
matrix = [(17, 15, 12),
          (53, np.NaN, 10),
          (46, 34, 11),
          (35, 45, np.NaN),
          (76, 26, 13)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))
# Get a series containing maximum value of each column without skipping NaN
maxValuesObj = dfObj.max(skipna=False)
print('Maximum value in each column including NaN: ')
print(maxValuesObj)

Output:

Maximum value in each column including NaN:
x 76.0
y NaN
z NaN

So in the above example, you can see that we have passed the ‘skipna=False’ in the max() function, So it included the NaN while searching for NaN.

If there is any NaN in the column then it will be considered as the maximum value of that column.

Get maximum values of a single column or selected columns

So for getting a single column maximum value we have to select that column and apply the max() function in it,

import pandas as pd
import numpy as np
# List of Tuples
matrix = [(17, 15, 12),
          (53, np.NaN, 10),
          (46, 34, 11),
          (35, 45, np.NaN),
          (76, 26, 13)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))
# Get maximum value of a single column 'y'
maxValue = dfObj['y'].max()
print("Maximum value in column 'y': " , maxValue)

Here you can see that we have passed y  maxValue = dfObj['y'].max()for getting max value in that column.

Output:

Maximum value in column 'y': 45.0

We can also pass the list of column names instead of passing single column like.,

import pandas as pd
import numpy as np
# List of Tuples
matrix = [(17, 15, 12),
          (53, np.NaN, 10),
          (46, 34, 11),
          (35, 45, np.NaN),
          (76, 26, 13)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))
# Get maximum value of a single column 'y'
maxValue = dfObj[['y', 'z']].max()
print("Maximum value in column 'y' & 'z': ")
print(maxValue)

Output:

Maximum value in column 'y' & 'z':
y 45.0
z 13.0

Get row index label or position of maximum values of every column

DataFrame.idxmax()

So in the above examples, you have seen how to get the max value of rows and columns but what if we want to know the index position of that row and column whereas the value is maximum, by using dataframe.idxmax() we get the index position.

Syntax-

DataFrame.idxmax(axis=0, skipna=True)

Get row index label of Maximum value in every column

import pandas as pd
import numpy as np
# List of Tuples
matrix = [(17, 15, 12),
          (53, np.NaN, 10),
          (46, 34, 11),
          (35, 45, np.NaN),
          (76, 26, 13)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))
# get the index position of max values in every column
maxValueIndexObj = dfObj.idxmax()
print("Max values of columns are at row index position :")
print(maxValueIndexObj)

Output:

Max values of columns are at row index position :
x e
y d
z e
dtype: object

So here you have seen it showed the index position of the column where max value exists.

Get Column names of Maximum value in every row

import pandas as pd
import numpy as np
# List of Tuples
matrix = [(17, 15, 12),
          (53, np.NaN, 10),
          (46, 34, 11),
          (35, 45, np.NaN),
          (76, 26, 13)
          ]
# Create a DataFrame object
dfObj = pd.DataFrame(matrix, index=list('abcde'), columns=list('xyz'))
# get the column name of max values in every row
maxValueIndexObj = dfObj.idxmax(axis=1)
print("Max values of row are at following columns :")
print(maxValueIndexObj)

Output:

Max values of row are at following columns :
a x
b x
c x
d y
e x
dtype: object

So here you have seen it showed the index position of a row where max value exists.

Conclusion:

So in this article, we have seen how to find maximum value & position in rows or columns of a Dataframe and its index position. Thank you!

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Find Elements in a Dataframe

Pandas: Find maximum values & position in columns or rows of a Dataframe | How to find the max value of a pandas DataFrame column in Python? Read More »

Get first key-value pair from a Python Dictionary

Finding first key-value pair from a Dictionary in Python

In this article, we will see of different methods by which we can fetch the first key-value pair of a dictionary. We will also discuss how to get first N pairs and any number of key-value pairs from a dictionary.

Getting first key-value pair of dictionary in python using iter() & next() :

In python, iter() function creates a iterator object of the the iterable sequence of key-value pairs from dictionary and by calling next() function we can get the first key-value pair.

# Program :

dict_eg = {
    'Sachin' : 10,
    "Gayle"  : 333,
    'Kohil'  : 18,
    'Murali' : 800,
    'Dhoni'  : 7,
    'AB'     : 17
}
# Get the first key-value pair in dictionary
dict_eg = next(iter((dict_eg.items())) )
print('The first Key Value Pair in the Dictionary is:')
print(dict_eg)
print('First Key: ', dict_eg[0])
print('First Value: ', dict_eg[1])
Output :
The first Key Value Pair in the Dictionary is:
('Sachin', 10)
First Key:  Sachin
First Value:  10

Get first key-value pair of dictionary using list :

In python, items() function in dictionary returns the iterable sequence of all key-value pairs. Then by creating a list from all key-value pairs in dictionary and by selecting the first item we will get first key-value pair of dictionary.

# Program :

dict_eg = {
    'Sachin' : 10,
    "Gayle"  : 333,
    'Kohil'  : 18,
    'Murali' : 800,
    'Dhoni'  : 7,
    'AB'     : 17
}
# Get the first key-value pair in dictionary
dict_eg = list(dict_eg.items())[0]
print('First Key Value Pair of Dictionary:')
print(dict_eg)
print('Key: ', dict_eg[0])
print('Value: ', dict_eg[1])
Output :
First Key Value Pair of Dictionary:
('Sachin', 10)
Key:  Sachin
Value:  10

Getting the first N key-value pair of dictionary in python using list & slicing :

Here from similar process, we will create a list of key-value pairs from dictionary. We can get first ‘N’ items by list[:N] or any items by list[start:end].

# Program :

dict_eg = {
    'Sachin' : 10,
    "Gayle"  : 333,
    'AB'     : 17,
    'Murali' : 800,
    'Dhoni'  : 7,
    'AB'     : 17,
    'Kohil'  : 18
}
n = 5
# Get first 5 pairs of key-value pairs
firstN_pairs = list(dict_eg.items())[:n]
print('The first 5 Key Value Pairs of Dictionary are:')
for key,value in firstN_pairs:
    print(key, '::', value)
Output :
The first 5 Key Value Pairs of Dictionary are:
Dhoni :: 7
Kohil :: 18
Gayle :: 333
Sachin :: 10
AB :: 17

Getting the first N key-value pair of dictionary in python using itertools :

We can slice first ‘N’ entries from a sequence by itertools.islice(iterable, stop) after creating key-value pairs sequence from items() function.

# Program :

import itertools
dict_eg = {
    'Sachin' : 10,
    "Gayle"  : 333,
    'AB'     : 17,
    'Murali' : 800,
    'Dhoni'  : 7,
    'AB'     : 17,
    'Kohil'  : 18
}
n = 5
# Get first 5 pairs of key-value pairs
firstN_pairs = itertools.islice(dict_eg.items(), n)
print('The first 5 Key Value Pairs of Dictionary are:')
for key,value in firstN_pairs:
    print(key, '::', value)
Output :
The first 5 Key Value Pairs of Dictionary are:
Murali :: 800
AB :: 17
Sachin :: 10
Dhoni :: 7
Kohil :: 18

Get first key-value pair from a Python Dictionary Read More »

How to Find an Element in Tuple by Value

Python : How to Find an Element in Tuple by Value

Tuples are variable types that allow you to store multiple items in a single variable. Tuple is one of Python’s four built-in data types for storing data collections. The other three are List, Set, and Dictionary, each with unique properties and applications. Tuples are collections that are both ordered and immutable.

Given a tuple, the task is to determine whether or not a given element is present in the tuple.

Examples:

Input:

given_tuple =( 3,4,5,6,2) element =2

Output:

The item is found in the tuple

Search an element in Tuple by value

There are several ways to search an element in tuple some of them are:

Method #1:Using for loop

This is a brute force method for completing this task. We iterate through the tuple and check each element to see if it is given element. If the element matches then written true else return false.

Below is the implementation:

# function which returns true if given element is found in tuple
def searchElement(given_tuple, element):
    # using for loop to traverse the tuple
    for value in given_tuple:
        # if the given element is equal to the value then return true
        if(value == element):
            return True
    # if the element is not found in tuple then return False
    return False


# given tuple
given_tuple = (3, 4, 5, 6, 2)
# given element
element = 2
# passing tuple and element to searchElement function
if(searchElement(given_tuple, element)):
    print("The item is found in the tuple")
else:
    print("The specified element does not exist in the tuple")

Output:

The item is found in the tuple

Method #2:Using in operator

Using the in operator is the most Pythonic way to accomplish this task. It is a one-liner that is recommended for this task.

Below is the implementation:

# function which returns true if given element is found in tuple
def searchElement(given_tuple, element):
    #using in operator
    # if the given eleement is present then return true
    if element in given_tuple:
        return True
    else:
        return False


# given tuple
given_tuple = (3, 4, 5, 6, 2)
# given element
element = 2
# passing tuple and element to searchElement function
if(searchElement(given_tuple, element)):
    print("The item is found in the tuple")
else:
    print("The specified element does not exist in the tuple")

Output:

The item is found in the tuple

Method #3:Using not in operator

Another method for searching for an element in a tuple is to use the not in operator.

If the element is not present in the tuple, it returns True otherwise, it returns False.

Below is the implementation:

# function which returns true if given element is found in tuple
def searchElement(given_tuple, element):
    #using in operator
    # if the given eleement is not present then return false
    if element not in given_tuple:
        return False
    else:
        return True


# given tuple
given_tuple = (3, 4, 5, 6, 2)
# given element
element = 2
# passing tuple and element to searchElement function
if(searchElement(given_tuple, element)):
    print("The item is found in the tuple")
else:
    print("The specified element does not exist in the tuple")

Output:

The item is found in the tuple

Method #4:Using count() function

Tuple has another member function count(),

given_tuple.count(element)

It returns the number of occurrences of element in the tuple.

If the count of an element is greater than zero, the element is present in the tuple .otherwise, the element is not present in the tuple.

Below is the implementation:

# function which returns true if given element is found in tuple
def searchElement(given_tuple, element):
    # using count() function to count the element in tuple
    element_count = given_tuple.count(element)
    # if the count is greater than 0 then return True
    if element_count > 0:
        return True
    else:
        return False


# given tuple
given_tuple = (3, 4, 5, 6, 2)
# given element
element = 2
# passing tuple and element to searchElement function
if(searchElement(given_tuple, element)):
    print("The item is found in the tuple")
else:
    print("The specified element does not exist in the tuple")

Output:

The item is found in the tuple

Method #5:Using index function

Sometimes just checking if an element exists in a tuple isn’t enough ,we want to know where it appears for the first time in the tuple. A member function index is provided by Tuple ()
It returns the index of the first occurrence of x in the tuple. In addition, if an element is not found in a tuple, an exception ValueError is thrown.

Below is the implementation:

# given tuple
given_tuple = (3, 4, 5, 6, 2)
# given element
element = 2
try:
    # calculating the element's index
    elem_index = given_tuple.index(element)
    # printing the position
    print("The given element is found at the index", elem_index)
except:
    print("The specified element does not exist in the tuple")

Output:

The item is found in the tuple

Related Programs:

Python : How to Find an Element in Tuple by Value Read More »

How to Create a List of all the Values in a Dictionary

Python : How to Create a List of all the Values in a Dictionary ?

List:

A collection is a set of values that are arranged in a specific order. There could be different kinds of values. A list is a container that can be changed. Existing ones can be added to, deleted from, or changed as a result.

The Python list is a mathematical representation of the concept of a finite sequence. List items or list elements are the names given to the values in a list. A list may contain multiple instances of the same value. Each event is treated as a separate element.

Dictionary:

Dictionaries are Python’s implementation of an associative list, which can be a collection. A dictionary can be defined as a collection of key-value pairs that are kept together. Each key-value pair represents a key and its value.

Given a dictionary, the task is to convert the values of dictionary to list.

Examples:

Input:

dictionary = {'Hello': 400, 'BtechGeeks': 900, 'Platform': 200}

Output:

[400, 900, 200]

Create a List of all the Values in a Dictionary

1)Using list() and values() function

A dictionary class in Python has a function values() that returns an iterable sequence of all dictionary values. The list() function will return a list containing all of the dictionary’s values if we transfer this iterable sequence(dict values) to it.

Below is the implementation:

# given_dictionary 
dictionary = {'Hello': 400, 'BtechGeeks': 900, 'Platform': 200} 
# creating a list from dictionary values 
list_of_values = list(dictionary.values()) 
# print the newly created list 
print(list_of_values)

Output:

[400,900,200]

2)Making a list of duplicate dictionary values

Approach:

  1. Take a two empty lists named dup_values and uniq_values
  2. Traverse the values of the dictionary
  3. If the value is not in uniq_values then append the element to it.
  4. Else append that element to dup_values.
  5. Print the dup_values list

Below is the implementation:

# given_dictionary
dictionary = {'Hello': 200, 'BtechGeeks': 900,
              'Platform': 200, 'online': 400, 'python': 400}
# taking two empty list
uniq_values = []
dup_values = []
# Traverse the values of the dictionary
for value in dictionary.values():
    if value not in uniq_values:
        uniq_values.append(value)
    else:
        dup_values.append(value)

# print the duplicate values
print(dup_values)

Output:

[200, 400]

Related Programs:

Python : How to Create a List of all the Values in a Dictionary ? Read More »

Iterate over Dictionary and Remove items

Python: Iterate over Dictionary and Remove items

Dictionary:

Dictionary is a mutable built-in Python Data Structure. It is conceptually similar to List, Set, and Tuples. It is, however, indexed by keys rather than a sequence of numbers and can be thought of as associative arrays. On a high level, it consists of a key and its associated value. The Dictionary class in Python describes a hash-table implementation.

Given a dictionary , the task is to iterate over the dictionary and remove items based on the given condition.

Examples:

Input:

Condition: Remove all the elements whose value is divisible by 3

dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}

Output:

{'hello': 89, 'is': 100}

Traverse the Dictionary and Remove items

There are several ways to remove items from the dictionary some of them are:

Method #1:Using del Keyword

We may make a copy of the dictionary and iterate through it, deleting elements from the original dictionary as we go. For example, we have a string and integer dictionary. We want to iterate through this dictionary and delete items whose value is divisible by 3.

Below is the implementation:

# Given dictionary
dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}
# Traverse the dictionary by making copy of it
for key, value in dict(dictionary).items():
    # if the value of key is divisible by 3 then remove it from original dictionary
    if(value % 3 == 0):
        # if the value is divisible by 3 the remove it from dictionary using del keyword
        del dictionary[key]
# print the dictionary after removing all the elements which satisfies the condition
print(dictionary)

Output:

{'hello': 89, 'is': 100}

Method #2:Using pop()

Similarly to the del keyword, we can make a copy of the original dictionary and iterate through it. We can add a condition to each pair during iteration, and if the condition is met, we can remove the element from the original dictionary. However, instead of using the del keyword, we will use the pop() function to delete elements during iteration.

Below is the implementation:

# Given dictionary
dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}
# Traverse the dictionary by making copy of it
for key, value in dict(dictionary).items():
    # if the value of key is divisible by 3 then remove it from original dictionary
    if(value % 3 == 0):
        # if the value is divisible by 3 the remove it from dictionary using pop()
        dictionary.pop(key)
# print the dictionary after removing all the elements which satisfies the condition
print(dictionary)

Output:

{'hello': 89, 'is': 100}

Method #3:Using List Comprehension

We may use dictionary comprehension to filter dictionary items based on condition and return the new dictionary to the original reference variable.

Below is the implementation:

# Given dictionary
dictionary = {'hello': 89, 'this': 51, 'is': 100, 'BTechGeeks': 201}
# using list comprehension
dictionary = {key: value for key, value in dictionary.items()
              if value % 3 != 0}
# print the dictionary after removing all the elements which satisfies the condition
print(dictionary)

Output:

{'hello': 89, 'is': 100}

Explanation:

We iterated over all key-value pairs in the dictionary and generated a new dictionary that only included objects whose values were not divisible by 3. The new dictionary was then assigned to the initial reference variable. It gave the impression that we had removed objects from the dictionary whose values were divisible by 3.

 
Related Programs:

Python: Iterate over Dictionary and Remove items Read More »

Find Frequency of Each Character in String and their Indices and Finding Duplicate Characters in a String

Find Frequency of Each Character in String and their Indices and Finding Duplicate Characters in a String

String:

A string is a group of alphabets, words, or other characters. It is a primitive data structure that serves as the foundation for data manipulation. Python has a string class called str. Strings in Python are “immutable,” which means they can’t be modified once they’re formed. Because of the immutability of strings, we generate new strings as we go to represent computed values.

Given  a string, the task is to find frequency of each character in a string .

Examples:

Input:

string = "hello this is btech geeks online learning platform for underguate students"

Output:

Frequency of each character of the string is :
The frequency of character h is = 3
The frequency of character e is = 9
The frequency of character l is = 5
The frequency of character o is = 4
The frequency of character   is = 10
The frequency of character t is = 6
The frequency of character i is = 4
The frequency of character s is = 5
The frequency of character b is = 1
The frequency of character c is = 1
The frequency of character g is = 3
The frequency of character k is = 1
The frequency of character n is = 6
The frequency of character a is = 3
The frequency of character r is = 4
The frequency of character p is = 1
The frequency of character f is = 2
The frequency of character m is = 1
The frequency of character u is = 3
The frequency of character d is = 2

Finding count of Each Character in a String and their Indices and Finding Duplicate Characters in a given String

There are several ways to find frequency of each character in a string some of them are:

1)Using Counter() function  to print frequency of each character in given string

Counter is a subclass of dict and a set. Counter() takes an iterable object as an argument and stores its elements as keys and their frequencies as values. As a result, if we transfer a string in collections. Counter() will then return a Counter class object with all characters in the string as keys and their frequency in the string as values.

Below is the implementation:

# importing Counter function from collections
from collections import Counter
# Given string
string = "hello this is btech geeks online learning platform for underguate students"
# Using counter() function to calculate frequency of each character of the string
freq = Counter(string)
print("Frequency of each character of the string is :")
# Traverse the freq dictionary and print their respective count
for key in freq:
    print("The frequency of character", key, "is =", freq[key])

Output:

Frequency of each character of the string is :
The frequency of character h is = 3
The frequency of character e is = 9
The frequency of character l is = 5
The frequency of character o is = 4
The frequency of character   is = 10
The frequency of character t is = 6
The frequency of character i is = 4
The frequency of character s is = 5
The frequency of character b is = 1
The frequency of character c is = 1
The frequency of character g is = 3
The frequency of character k is = 1
The frequency of character n is = 6
The frequency of character a is = 3
The frequency of character r is = 4
The frequency of character p is = 1
The frequency of character f is = 2
The frequency of character m is = 1
The frequency of character u is = 3
The frequency of character d is = 2

2)Using regex to find frequency and indices of all characters in a string

We will construct a regex pattern to fit all of the alphanumeric characters in the string,

Make a Regex pattern that matches alphanumeric characters.

regex_Pattern = re.compile('[a-zA-Z0-9]')

Iterate over all of the above-mentioned pattern matches in the string using pattern.

finditer() and generate dictionaries of each character’s frequency count and index position in the string.

Below is the implementation:

import re
# Given string
string = "hello this is btech geeks online learning platform for underguate students"
# regex pattern
regex_Pattern = re.compile('[a-zA-Z0-9]')
# Iterate through the string's alphanumeric characters which matches the regex pattern
# While iterating, keep the frequency count of each character in a dictionary updated.
matchiterator = regex_Pattern.finditer(string)
charfrequency = {}
indices = {}
for matchchar in matchiterator:
    charfrequency[matchchar.group()] = charfrequency.get(
        matchchar.group(), 0) + 1
    indices[matchchar.group()] = indices.get(
        matchchar.group(), []) + [matchchar.start()]
print("Frequency and indices of each character in the string is :")
# Traverse the charfrquency dictionary and print their respective count and indices
for key in charfrequency:
    print("The frequency of character", key, "is =",
          charfrequency[key], " ; Indices of occurrence = ", indices[key])

Output:

Frequency and indices of each character in the string is :
The frequency of character h is = 3  ; Indices of occurrence =  [0, 7, 18]
The frequency of character e is = 9  ; Indices of occurrence =  [1, 16, 21, 22, 31, 34, 58, 64, 70]
The frequency of character l is = 5  ; Indices of occurrence =  [2, 3, 28, 33, 43]
The frequency of character o is = 4  ; Indices of occurrence =  [4, 26, 47, 52]
The frequency of character t is = 6  ; Indices of occurrence =  [6, 15, 45, 63, 67, 72]
The frequency of character i is = 4  ; Indices of occurrence =  [8, 11, 29, 38]
The frequency of character s is = 5  ; Indices of occurrence =  [9, 12, 24, 66, 73]
The frequency of character b is = 1  ; Indices of occurrence =  [14]
The frequency of character c is = 1  ; Indices of occurrence =  [17]
The frequency of character g is = 3  ; Indices of occurrence =  [20, 40, 60]
The frequency of character k is = 1  ; Indices of occurrence =  [23]
The frequency of character n is = 6  ; Indices of occurrence =  [27, 30, 37, 39, 56, 71]
The frequency of character a is = 3  ; Indices of occurrence =  [35, 44, 62]
The frequency of character r is = 4  ; Indices of occurrence =  [36, 48, 53, 59]
The frequency of character p is = 1  ; Indices of occurrence =  [42]
The frequency of character f is = 2  ; Indices of occurrence =  [46, 51]
The frequency of character m is = 1  ; Indices of occurrence =  [49]
The frequency of character u is = 3  ; Indices of occurrence =  [55, 61, 68]
The frequency of character d is = 2  ; Indices of occurrence =  [57, 69]

3)Using Counter to find Duplicate characters in the given string

Now, use collections to find all of the duplicate characters in this string. Counter() is used to determine the frequency of of character in a string, and characters with a frequency greater than 1 are considered duplicates.

Below is the implementation:

# importing Counter function from collections
from collections import Counter
# Given string
string = "hello this is btech geeks online learning platform for underguate students"
# Using counter() function to calculate frequency of each character of the string
freq = Counter(string)
print("Printing duplicate characters in the given string :")
# Traverse the freq dictionary and print the duplicate characters
for key in freq:
    # if the freq of character is greater than 1 then it is duplicate character so we print it
    if(freq[key] > 1):
        print(key)

Output:

Printing duplicate characters in the given string :
h
e
l
o
 
t
i
s
g
n
a
r
f
u
d

 
Related Programs:

Find Frequency of Each Character in String and their Indices and Finding Duplicate Characters in a String Read More »

Python Variables

Python – Variables

Python is not a “statically typed” language. We do not need to declare variables or their types before using them. When we first assign a value to a variable, it is generated. A variable is a name that is assigned to a memory location. It is the fundamental storage unit in a program.

In this post, we’ll go over what you need to know about variables in Python.

Variables in Python Language

1)Variable

Variables are simply reserved memory locations for storing values. This means that when you construct a variable, you reserve memory space.

The interpreter allocates memory and specifies what can be stored in reserved memory based on the data type of a variable. As a result, you can store integers, decimals, or characters in variables by assigning various data types to them.

2)Important points about variables

  • In Python we don’t have to give the type of information when defining a variable, unlike the other programming languages (C++ or Java). The variable form is assumed by Python implicitly on the basis of a variable value.
  • During program execution, the value stored in a variable may be modified.
  • A variable is simply the name given to a memory location, all operations performed on the variable have an impact on that memory location.

3)Initializing the value of the variable

There is no clear statement to reserve the memory space for Python variables. When you assign a value to a variable, the declaration occurs automatically. To allocate values to the variables, the same sign (=) is used.

The = operator’s left operand is the variable name and the operand’s right is the value in the variable. The = operator is the variable value.

Examples:

A=100
b="Hello"
c=4.5

4)Memory and reference

A variable in Python resembles a tag or a reference that points to a memory object.

As an example,

k=”BTechGeeks”

‘BTechGeeks’ is an string object in the memory, and k is a reference or tag the indicates that memory object.

5)Modifying the variable value

Let us try this:

p=4.5
p="Cirus"

Initially, p pointed to a float object, but now it points to a string object in memory. The variable’s type also changed; originally, it was a decimal (float), but when we assigned a string object to it, the type of p changed to str, i.e., a string.

If there is an object in memory but no vector pointing to it, the garbage collector can automatically free it. We forced the variable p to point to a string object, as in the preceding example, and then float 4.5 was left in memory with no variable pointing to it. The object was then immediately released by the garbage collector.

6)Assigning one variable with another variable

We can assign the value of one variable with another variable like

p="BtechGeeks"
q=p

Both the p and q variables now point to the same string object, namely, ‘BTechGeeks.’

Below is the implementation:

p = "BTechGeeks"
# assign variable q with p
q = p
# print the values
print("The value of p :", p)
print("The value of q :", q)

Output:

The value of p : BTechGeeks
The value of q : BTechGeeks

7)The following are the rules for creating variables in Python

  • A variable name must begin with a letter or an underscore.
  • A number cannot be the first character in a variable name.
  • Variable names can only contain alphanumeric characters and underscores (A-z, 0-9, and _ ).
  • Case matters when it comes to variable names (flag, Flag and FLAG Aare three different variables).
  • The reserved terms (keywords) are not permitted to be used in naming the variable.

Related Programs:

Python – Variables Read More »

List Comprehension vs Generator Expression Explained with Examples

Python : List Comprehension vs Generator Expression Explained with Examples

Generator Expression:

To build iterators in Python, we can use both standard functions and generators. Generators are written in the same way as regular functions, except we use yield() instead of return() to return a value. It is more effective as an iterator implementation tool. It is simpler and more convenient to implement since it provides element evaluation on demand. Unlike normal functions, which stop when they experience a return statement, generators use a yield statement, which saves the state of the function from the previous call and can be picked up or resumed the next time we call a generator function. Another significant benefit of the generator over a list is that it consumes much less memory.

List Comprehension:

When you want to generate a new list based on the values of an existing list, list comprehension provides a shorter syntax.

List Comprehension vs Generator Expression

1)Use of Generator Expression

Often we only want to work on a few elements, and we only want to work on them one at a time. In such cases, we can avoid storing unnecessary elements in memory by using lists, sets, or tuples.

As an example, suppose we want to measure the sum of all the numbers in the range 1 to 10000 that are divisible by 2 and 3.

i)Using List Comprehension

Below is the implementation:

# Create a list of numbers in the range 1 to 10000 that are divisible by 2 and 3.
numslist = [n for n in range(10000) if n % 2 == 0 and n % 3 == 0]
# Calculate the sum of the list
list_sum = 0
for element in numslist:
    list_sum += element
print('Sum of list elements = ', list_sum)

Output:

Sum of list elements =  8331666

We made a list of numbers, numslist, and then iterated over each variable in the list to compute the count. Creating a list of too many elements and keeping them in memory was wasteful in this case, and it could have been prevented by using Generators.

ii)Using Generator Expression to calculate the sum

Since we just needed the sum, there is no need to make a long list of these numbers and then calculate the sum. The most effective method is to create a Generator for these selected numbers and then iterate over them one by one using the generator object to obtain the sum. This way, we won’t have to hold all of the elements that aren’t required in memory.

Below is the implementation:

def checkNumber():
    # A generator that produces multiples of 2 and 3 in the range 0 to 10000.
    for number in range(10000):
        if number % 2 == 0 and number % 3 == 0:
            yield number


# initializing the numbobj with  the value returned by checkNumber
numObj = checkNumber()
# Calculate the sum of the list
list_sum = 0
for element in numObj:
    list_sum += element
print('Sum using generator expression = ', list_sum)

Output:

Sum using generator expression =  8331666

2)Generator Expression

A generator expression is used in Python to create Generators. In syntax, it appears to be List comprehension, however ( ) are used instead of [].
Let’s use Generator Expression to find the sum of numbers divisible by 2 and 3 in the range 1 to 10000.

Make a Generator expression that yields a Generator entity.

Below is the implementation:

# initializing the numbobj using generator expression
numObj = (n for n in range(10000) if n % 2 == 0 and n % 3 == 0)
# Calculate the sum of the list
list_sum = 0
for element in numObj:
    list_sum += element
print('Sum using generator expression = ', list_sum)

Output:

Sum using generator expression =  8331666

3)Generator Expression vs List Comprehension

Except for the brackets, the syntax of a Generator expression is identical to that of a List Comprehension, but the key distinction between the two is that the latter returns a Generator object rather than a list. We should use Generators when we just want to loop over the items one at a time and avoid having redundant elements in memory, as shown in the preceding examples.
Related Programs:

Python : List Comprehension vs Generator Expression Explained with Examples Read More »

How to use Global Variables in a Function

Python : How to use Global Variables in a Function ?

In this article, we’ll look at the differences between local and global variables, as well as how to access and change both global and local variables with the same name within a function.

Global variables and local variables are the two types of variables.
Global variables have a scope that spans the entire program, while local variables have a scope that is restricted to the role in which they are specified.

Use of Global Variables in a Function

1)Global Variable

In a program, a global variable is a variable specified outside of the subroutine or function. It has a global reach, which means it would be useful for the duration of the program. As a result, unless it is shadowed, it can be accessed within the program by any feature specified within the program.

2)Advantages of Global Variable

  • The global variable can be accessed from any function or module in a program.
  • You only need to declare global variables once outside of the modules.
  • It is suitable for storing “constants” because it lets you maintain continuity.
  • When multiple functions need to access the same data, a global variable comes in handy.

3)Disadvantages of Global Variable

  • If there are so many global variables declared, they will remain in memory until the program is finished. This can result in an Out of Memory error.
  • Any feature has the ability to change data. The value of the global variable can be changed by any expression in the program. In multi-tasking situations, this may lead to unexpected outcomes.
  • If global variables are no longer used as a result of code refactoring, you’ll need to update all of the modules that use them.

4)Local Variable vs Global Variable

A local variable is defined within a function, while a global variable is defined outside of any function or class, i.e. in global space. A global variable may be used in any function, while a local variable has only the scope of the function in which it is specified.

Example:

# declaring global variable
weight = 60

def test():
    # declaring  local variable
    age = 19
    print('weight = ', weight)
    print('age = ', age)

test()

Output:

weight =  60
age =  19

Here, ‘weight’ is a global variable that can be accessed from within function test() too, while ‘age’ is a local variable that can only be accessed from within function test().

5)Global Variables and Local Variables with same name

# declaring global variable
weight = 60

def test():
    # declaring  local variable with same name
    weight = 19

# printing the weight before calling test()
print('weight = ', weight)
test()
# printing the weight after calling test()
print('weight = ', weight)

Output:

weight =  60
weight =  60

Explanation:

In this case, ‘weight’ is a global variable, and the test() function has a local variable of the same name. If both variables have the same name, a function would prefer the local variable over the global variable. As a result, in the preceding code, when we changed the ‘weight’ variable within the function, the change was not reflected outside the function. Since total variable is treated as a local variable within feature test().

6)Use of global Keyword

If your function has a local variable with the same name as a global variable and you want to change the global variable within the function, use the ‘global’ keyword before the variable name at the start of the function.

It will cause the function to refer to the global variable total whenever it is called.

Below is the implementation:

# declaring global variable
weight = 60

def test():
    # within function, refer to global variable 'weight'
    global weight
    if(weight > 50):
        weight = 70

# printing the weight before calling test()
print('weight = ', weight)
test()
# printing the weight after calling test()
print('weight = ', weight)

Output:

weight =  60
weight =  70

As you can see, the changes you made to the global variable total are now noticeable outside of the feature as well.

When the global keyword is used with a variable inside a function, the local variable is hidden.

7)Using globals()

Since the ‘global’ keywords hide the local variable of the same name, there is another way to access both the local and global variables within a function, which is the global() function.
globals() returns a dictionary of elements in the current module, which we can use to access and change global variables without using the ‘global’ keyword.

# declaring global variable
weight = 60

def test():
    # golbal list
    global_list = globals()
    global_list['weight'] = 20
    weight = 30
    print('Local weight = ', weight)

# printing the weight before calling test()
print('weight = ', weight)
test()
# printing the weight after calling test()
print('weight = ', weight)

Output:

weight =  60
Local weight =  30
weight =  20

Explanation:

As you can see, we have a local variable and a global variable with the same name, complete, and we changed both within the feature. Instead of using the keyword ‘global,’ use the dictionary returned by globals() to refer to a global variable. It will not conceal a local variable within the feature.
Related Programs:

Python : How to use Global Variables in a Function ? Read More »

Python-Add column to dataframe in Pandas

Python: Add column to dataframe in Pandas ( based on other column or list or default value)

In this tutorial, we are going to discuss different ways to add columns to the dataframe in pandas. Moreover, you can have an idea about the Pandas Add Column, Adding a new column to the existing DataFrame in Pandas and many more from the below explained various methods.

Pandas Add Column

Pandas is one such data analytics library created explicitly for Python to implement data manipulation and data analysis. The Pandas library made of specific data structures and operations to deal with numerical tables, analyzing data, and work with time series.

Basically, there are three ways to add columns to pandas i.e., Using [] operator, using assign() function & using insert().

We will discuss it all one by one.

First, let’s create a dataframe object,

import pandas as pd
# List of Tuples
students = [('Rakesh', 34, 'Agra', 'India'),
            ('Rekha', 30, 'Pune', 'India'),
            ('Suhail', 31, 'Mumbai', 'India'),
            ('Neelam', 32, 'Bangalore', 'India'),
            ('Jay', 16, 'Bengal', 'India'),
            ('Mahak', 17, 'Varanasi', 'India')]
# Create a DataFrame object
df_obj = pd.DataFrame(students,
                      columns=['Name', 'Age', 'City', 'Country'],
                      index=['a', 'b', 'c', 'd', 'e', 'f'])
print(df_obj )

Output:

    Name    Age  City        Country
a  Rakesh  34     Agra        India
b  Rekha   30     Pune        India
c  Suhail    31    Mumbai   India
d  Neelam 32   Bangalore India
e  Jay         16   Bengal      India
f  Mahak    17  Varanasi     India

Do Check:

Add column to dataframe in pandas using [] operator

import pandas as pd
# List of Tuples
students = [('Rakesh', 34, 'Agra', 'India'),
            ('Rekha', 30, 'Pune', 'India'),
            ('Suhail', 31, 'Mumbai', 'India'),
            ('Neelam', 32, 'Bangalore', 'India'),
            ('Jay', 16, 'Bengal', 'India'),
            ('Mahak', 17, 'Varanasi', 'India')]
# Create a DataFrame object
df_obj = pd.DataFrame(students,
                      columns=['Name', 'Age', 'City', 'Country'],
                      index=['a', 'b', 'c', 'd', 'e', 'f'])

# Add column with Name Score
df_obj['Score'] = [10, 20, 45, 33, 22, 11]
print(df_obj )

Output:

      Name     Age   City        Country   Score
a    Rakesh    34    Agra          India      10
b    Rekha     30    Pune          India      20
c    Suhail     31     Mumbai    India      45
d    Neelam  32    Bangalore  India      33
e    Jay         16     Bengal       India      22
f    Mahak    17    Varanasi     India       11

So in the above example, you have seen we have added one extra column ‘score’ in our dataframe. So in this, we add a new column to Dataframe with Values in the list. In the above dataframe, there is no column name ‘score’ that’s why it added if there is any column with the same name that already exists then it will replace all its values.

Add new column to DataFrame with same default value

import pandas as pd
# List of Tuples
students = [('Rakesh', 34, 'Agra', 'India'),
            ('Rekha', 30, 'Pune', 'India'),
            ('Suhail', 31, 'Mumbai', 'India'),
            ('Neelam', 32, 'Bangalore', 'India'),
            ('Jay', 16, 'Bengal', 'India'),
            ('Mahak', 17, 'Varanasi', 'India')]
# Create a DataFrame object
df_obj = pd.DataFrame(students,
                      columns=['Name', 'Age', 'City', 'Country'],
                      index=['a', 'b', 'c', 'd', 'e', 'f'])

df_obj['Total'] = 100
print(df_obj)

Output:

         Name    Age    City     Country      Total
a       Rakesh   34    Agra       India          100
b       Rekha    30    Pune       India          100
c       Suhail     31   Mumbai  India          100
d       Neelam  32  Bangalore India        100
e       Jay          16  Bengal       India        100
f       Mahak     17 Varanasi     India        100

So in the above example, we have added a new column ‘Total’ with the same value of 100 in each index.

Add column based on another column

Let’s add a new column ‘Percentage‘ where entrance at each index will be added by the values in other columns at that index i.e.,

df_obj['Percentage'] = (df_obj['Marks'] / df_obj['Total']) * 100
df_obj

Output:

    Name  Age       City    Country  Marks  Total  Percentage
a   jack   34     Sydeny  Australia     10     50        20.0
b   Riti   30      Delhi      India     20     50        40.0
c  Vikas   31     Mumbai      India     45     50        90.0
d  Neelu   32  Bangalore      India     33     50        66.0
e   John   16   New York         US     22     50        44.0
f   Mike   17  las vegas         US     11     50        22.0

Append column to dataFrame using assign() function

So for this, we are going to use the same dataframe which we have created in starting.

Syntax:

DataFrame.assign(**kwargs)

Let’s add columns in DataFrame using assign().

import pandas as pd
# List of Tuples
students = [('Rakesh', 34, 'Agra', 'India'),
            ('Rekha', 30, 'Pune', 'India'),
            ('Suhail', 31, 'Mumbai', 'India'),
            ('Neelam', 32, 'Bangalore', 'India'),
            ('Jay', 16, 'Bengal', 'India'),
            ('Mahak', 17, 'Varanasi', 'India')]
# Create a DataFrame object
df_obj = pd.DataFrame(students,
                      columns=['Name', 'Age', 'City', 'Country'],
                      index=['a', 'b', 'c', 'd', 'e', 'f'])
mod_fd = df_obj.assign(Marks=[10, 20, 45, 33, 22, 11])
print(mod_fd)

Output:

Add-a-column-using-assign

It will return a new dataframe with a new column ‘Marks’ in that Dataframe. Values provided in the list will be used as column values.

Add column in DataFrame based on other column using lambda function

In this method using two existing columns i.e, score and total value we are going to create a new column i.e..’ percentage’.

import pandas as pd
# List of Tuples
students = [('Rakesh', 34, 'Agra', 'India'),
            ('Rekha', 30, 'Pune', 'India'),
            ('Suhail', 31, 'Mumbai', 'India'),
            ('Neelam', 32, 'Bangalore', 'India'),
            ('Jay', 16, 'Bengal', 'India'),
            ('Mahak', 17, 'Varanasi', 'India')]
# Create a DataFrame object
df_obj = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Country'],
                      index=['a', 'b', 'c', 'd', 'e', 'f'])
df_obj['Score'] = [10, 20, 45, 33, 22, 11]
df_obj['Total'] = 100
df_obj = df_obj.assign(Percentage=lambda x: (x['Score'] / x['Total']) * 100)
print(df_obj)

Output:

Add-column-based-on-another-column

Add new column to Dataframe using insert()

import pandas as pd
# List of Tuples
students = [('Rakesh', 34, 'Agra', 'India'),
            ('Rekha', 30, 'Pune', 'India'),
            ('Suhail', 31, 'Mumbai', 'India'),
            ('Neelam', 32, 'Bangalore', 'India'),
            ('Jay', 16, 'Bengal', 'India'),
            ('Mahak', 17, 'Varanasi', 'India')]
# Create a DataFrame object
df_obj = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Country'],
                      index=['a', 'b', 'c', 'd', 'e', 'f'])
# Insert column at the 2nd position of Dataframe
df_obj.insert(2, "Marks", [10, 20, 45, 33, 22, 11], True)
print(df_obj)

Output:

add-a-column-using-insert

 

In other examples, we have added a new column at the end of the dataframe, but in the above example, we insert a new column in between the other columns of the dataframe, then we can use the insert() function.

Add a column to Dataframe by dictionary

import pandas as pd
# List of Tuples
students = [('Rakesh', 34, 'Agra', 'India'),
            ('Rekha', 30, 'Pune', 'India'),
            ('Suhail', 31, 'Mumbai', 'India'),
            ('Neelam', 32, 'Bangalore', 'India'),
            ('Jay', 16, 'Bengal', 'India'),
            ('Mahak', 17, 'Varanasi', 'India')]
# Create a DataFrame object
df_obj = pd.DataFrame(students, columns=['Name', 'Age', 'City', 'Country'],
                      index=['a', 'b', 'c', 'd', 'e', 'f'])
ids = [11, 12, 13, 14, 15, 16]
# Provide 'ID' as the column name and for values provide dictionary
df_obj['ID'] = dict(zip(ids, df_obj['Name']))
print(df_obj)

Output:

Add-a-column-using-dictionary

Want to expert in the python programming language? Exploring Python Data Analysis using Pandas tutorial changes your knowledge from basic to advance level in python concepts.

Read more Articles on Python Data Analysis Using Padas – Add Contents to a Dataframe

Python: Add column to dataframe in Pandas ( based on other column or list or default value) Read More »