Python

Pandas : Merge Dataframes on specific columns or on index in Python – Part 2

Merge Dataframes on specific columns or on index in Python

In this article, we will learn to merge dataframes on basis of given columns or index.

Dataframe.merge() :

Dataframe class of Python’s Pandas library provide a function i.e. merge() which helps in merging of two DataFrames.

Syntax: DataFrame.merge(right, how='inner', on=None, left_on=None, right_on=None, left_index=False, right_index=False, sort=False, suffixes=('_x', '_y'), copy=True, indicator=False, validate=None)

Arguments:-

  • right : A dataframe that is to be merged with the calling dataframe.
  • how : (Merge type). Some values are : left, right, outer, inner. Its default value is ‘inner’. If the two dataframes contains different columns, then based how value, columns will be considered accordingly for merging.
  • on : It is the column name on which merge will be done. If not provided then merged done on basis of indexes.
  • left_on : Column in left dataframe where merging is to be done.
  • right_on : Column in right dataframe, where merging is to be done.
  • left_index : (bool), default is False (If found True index index from left dataframe selected as join key)
  • right_index : (bool), default is False (If found True index index from right dataframe selected as join key)
  • suffixes : tuple of (str, str), default (‘_x’, ‘_y’) (Suffix that is to be applied on overlapping columns in left and right dataframes respectively.)

Merging Dataframe on a given column name as join key :

Let’s take a scenario where the columns names are same, but contents are different i.e. one column data is of int type and other column data is of string type. And if we apply merge() on them without passing any argument, it wouldn’t merge here. Here, we can merge dataframes on a single column by passing on argument in merge() function.

And as both dataframes have common column i.e. sponsered, so after merging columns are named by default. It will splitted by taking a suffix  i.e. Sponsered_x and Sponsered_y as left and right dataframe respectively.

import pandas as sc
# List of Tuples
players = [(15,'Smith','Pune', 17,12000),
            (99,'Rana', 'Mumbai', 20,2000),
            (51,'Jaydev','Kolkata', 22,25640),
            (31,'Shikhar','Hyderabad', 28,85410),
            (12,'Sanju','Rajasthan', 21,63180),
            (35,'Raina','Gujarat', 18,62790)
            ]
# Creation of DataFrame object
playDFObj = sc.DataFrame(players, columns=['JersyN','Name', 'Team', 'Age','Salary'], index=['I', 'II', 'III', 'IV', 'V', 'VI'])
print('DataFrame 1 : ')
print(playDFObj)
# List of Tuples
moreInfo = [(15, 13, 180000, 'Nissin') ,
           (99, 2, 195200, 'Jio') ,
           (51, 7, 15499, 'Lays') ,
           (31, 17, 654000, 'AmbujaC') ,
           (12, 5, 201000, 'AsianP') ,
           (35, 14, 741000, 'Airtel')
            ]
# Creation of DataFrame object
moreinfoObj = sc.DataFrame(moreInfo, columns=['JersyN', 'PLayingSince' , 'Salary', 'Sponsered'], index=['I', 'II', 'III', 'IV', 'V', 'VI'])
print('DataFrame 2 : ')
print(moreinfoObj)
# Merge two Dataframes on basis of common column by default INNER JOIN
mergedDataf = playDFObj.merge(moreinfoObj, on='JersyN')
print('After merging: ')
print(mergedDataf)
Output :

DataFrame 1 : 
 JersyN   Name    Team       Age    Salary
I    15    Smith      Pune         17    12000
II   99    Rana       Mumbai    20    2000
III  51    Jaydev    Kolkata      22   25640
IV  31   Shikhar   Hyderabad 28   85410
V  12    Sanju      Rajasthan   21   63180
VI  35    Raina     Gujarat       18   62790
DataFrame 2 : 
   JersyN PLayingSince   Salary       Sponsered
I    15             13            180000       Nissin
II    99            2              195200         Jio
III   51            7              15499          Lays
IV   31          17              654000     AmbujaC
V   12           5                201000       AsianP
VI   35          14              741000         Airtel
After merging: 
  JersyN     Name    Team            Age Salary_x    PLayingSince Salary_y Sponsered
0  15          Smith    Pune            17       12000       13              180000   Nissin
1  99          Rana     Mumbai       20       2000          2               195200   Jio
2  51          Jaydev   Kolkata        22       25640        7               15499    Lays
3  31          Shikhar  Hyderabad   28      85410       17              654000  AmbujaC
4  12          Sanju     Rajasthan     21      63180        5               201000  AsianP
5  5           Raina    Gujarat           18      62790       14              741000  Airtel

Merging Dataframe on a given column with suffix for similar column names :

In previous example, for common columns with dissimilar contents suffix x & y are added. We can also add our own custom suffix.

import pandas as sc
# List of Tuples
players = [(15,'Smith','Pune', 17,12000),
            (99,'Rana', 'Mumbai', 20,2000),
            (51,'Jaydev','Kolkata', 22,25640),
            (31,'Shikhar','Hyderabad', 28,85410),
            (12,'Sanju','Rajasthan', 21,63180),
            (35,'Raina','Gujarat', 18,62790)
            ]
# Creation of DataFrame object
playDFObj = sc.DataFrame(players, columns=['JersyN','Name', 'Team', 'Age','Sponsered'], index=['I', 'II', 'III', 'IV', 'V', 'VI'])
print('DataFrame 1 : ')
print(playDFObj)
# List of Tuples
moreInfo = [(15, 13, 180000, 'Nissin') ,
           (99, 2, 195200, 'Jio') ,
           (51, 7, 15499, 'Lays') ,
           (31, 17, 654000, 'AmbujaC') ,
           (12, 5, 201000, 'AsianP') ,
           (35, 14, 741000, 'Airtel')
            ]
# Creation of DataFrame object
moreinfoObj = sc.DataFrame(moreInfo, columns=['JersyN', 'PLayingSince' , 'Salary', 'Sponsered'], index=['I', 'II', 'III', 'IV', 'V', 'VI'])
print('DataFrame 2 : ')
print(moreinfoObj)
# Merge two Dataframes on basis of common column by default INNER JOIN
mergedDataf = playDFObj.merge(moreinfoObj, on='JersyN',suffixes=('_Price', '_Companies'))
print('After merging: ')
print(mergedDataf)
Output :
DataFrame 1 : 
  JersyN Name  Team          Age Sponsered
I 15       Smith   Pune           17   12000
II 99      Rana     Mumbai     20   2000
III 51     Jaydev   Kolkata      22   25640
IV 31    Shikhar  Hyderabad 28   85410
V 12     Sanju     Rajasthan   21   63180
VI 35    Raina     Gujarat      18    62790
DataFrame 2 : 
  JersyN PLayingSince Salary     Sponsered
I   15      13                 180000   Nissin
II  99     2                    195200    Jio
III  51     7                   15499      Lays
IV  31   17                   654000   AmbujaC
V  12     5                    201000    AsianP
VI  35   14                   741000     Airtel
After merging: 
JersyN Name Team ... PLayingSince Salary Sponsered_Companies
0 15 Smith Pune ... 13 180000 Nissin
1 99 Rana Mumbai ... 2 195200 Jio
2 51 Jaydev Kolkata ... 7 15499 Lays
3 31 Shikhar Hyderabad ... 17 654000 AmbujaC
4 12 Sanju Rajasthan ... 5 201000 AsianP
5 35 Raina Gujarat ... 14 741000 Airtel

Merging Dataframe different columns :

Now let’s take a scenario of changing name of JersyN column of a dataframe and try to merge it with another dataframe.

import pandas as sc
# List of Tuples
players = [(15,'Smith','Pune', 17,12000),
            (99,'Rana', 'Mumbai', 20,2000),
            (51,'Jaydev','Kolkata', 22,25640),
            (31,'Shikhar','Hyderabad', 28,85410),
            (12,'Sanju','Rajasthan', 21,63180),
            (35,'Raina','Gujarat', 18,62790)
            ]
# Creation of DataFrame object
playDFObj = sc.DataFrame(players, columns=['JersyN','Name', 'Team', 'Age','Salary'], index=['I', 'II', 'III', 'IV', 'V', 'VI'])
print('DataFrame 1 : ')
print(playDFObj)
# List of Tuples
moreInfo = [(15, 13, 180000, 'Nissin') ,
           (99, 2, 195200, 'Jio') ,
           (51, 7, 15499, 'Lays') ,
           (31, 17, 654000, 'AmbujaC') ,
           (12, 5, 201000, 'AsianP') ,
           (35, 14, 741000, 'Airtel')
            ]
# Creation of DataFrame object
moreinfoObj = sc.DataFrame(moreInfo, columns=['JersyN', 'PLayingSince' , 'Salary', 'Sponsered'], index=['I', 'II', 'III', 'IV', 'V', 'VI'])
print('DataFrame 2 : ')
print(moreinfoObj)
# Rename column JersyN to ShirtN
moreinfoObj.rename(columns={'JersyN': 'ShirtN'}, inplace=True)
# Merge two Dataframes on basis of common column by default INNER JOIN
mergedDataf = playDFObj.merge(moreinfoObj, left_on='JersyN', right_on='ShirtN')
print('After merging: ')
print(mergedDataf)
Output ;
DataFrame 1 : 
 JersyN Name Team         Age   Salary
I 15    Smith     Pune           17   12000
II 99   Rana      Mumbai      20    2000
III 51  Jaydev   Kolkata        22    25640
IV 31  Shikhar  Hyderabad  28    85410
V 12   Sanju     Rajasthan     21   63180
VI 35  Raina     Gujarat        18    62790
DataFrame 2 : 
   JersyN  PLayingSince   Salary    Sponsered
I   15              13           180000    Nissin
II   99             2             195200    Jio
III  51             7             15499     Lays
IV  31           17             654000   AmbujaC
V  12             5              201000   AsianP
VI  35           14             741000   Airtel
After merging: 
JersyN Name Team        Age ... ShirtN PLayingSince Salary Sponsered_y
0 15 Smith   Pune            17 ...   15           13             180000   Nissin
1 99 Rana     Mumbai      20 ...   99            2              195200   Jio
2 51 Jaydev  Kolkata       22 ...    51            7              15499    Lays
3 31 Shikhar Hyderabad 28 ...    31            17            654000  AmbujaC
4 12 Sanju    Rajasthan    21 ...   12             5             201000  AsianP
5 35 Raina    Gujarat       18 ...    35            14            741000  Airtel

[6 rows x 9 columns]

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

Pandas : Merge Dataframes on specific columns or on index in Python – Part 2 Read More »

Pandas : Convert Dataframe column into an index using set_index() in Python

Converting Dataframe column into an index using set_index() in Python

In this article we will learn to convert an existing column of Dataframe to a index including various cases. We can implement this using set_index() function of Pandas Dataframe class.

DataFrame.set_index() :

Syntax:- DataFrame.set_index(self, keys, drop=True, append=False, inplace=False, verify_integrity=False)

Arguments:

  • Keys: Column names that we want to set as an index of dataframe.
  • drop: (bool), default is True
  1. Where found True, after converting as an index column is deleted
  2. Where found False, then column is not deleted
  • append: (bool), default is False (If passed as True, then adds the given column is added to the existing index, and if passed as False, then current Index is replaced with it.)
  • inplace: (bool), in default is False (If passed as True then makes changes in the calling dataframe object otherwise if it is False, then returns a copy of modified dataframe.)
  • verify_integrity: (bool), default is False
  1. If True, searches for duplicate entries in new index.
  • Dataframe has a default index and we can give a name e.g. SL
import pandas as sc
# List of Tuples
players = [('Smith', 15, 'Pune', 170000),
            ('Rana', 99, 'Mumbai', 118560),
            ('Jaydev', 51, 'Kolkata', 258741),
            ('Shikhar', 31, 'Hyderabad', 485169),
            ('Sanju', 12, 'Rajasthan', 150000),
            ('Raina', 35, 'Gujarat', 250000)
            ]
# Creation of DataFrame object
playDFObj = sc.DataFrame(players, columns=['Name', 'JerseyN', 'Team', 'Salary'])
# Renaming index of dataframe as 'SL'
playDFObj.index.rename('SL', inplace=True)
print('Original Dataframe: ')
print(playDFObj)
Output :
Original Dataframe: 
Name JerseyN Team Salary
SL 
0 Smith 15 Pune 170000
1 Rana 99 Mumbai 118560
2 Jaydev 51 Kolkata 258741
3 Shikhar 31 Hyderabad 485169
4 Sanju 12 Rajasthan 150000
5 Raina 35 Gujarat 250000

Converting a column of Dataframe into an index of the Dataframe :

Let’s try to convert of column Name into index of dataframe. We can implement this by passing that column name into set_index. Here the column names would be converted to ‘Name’ deleting old index.

Here it only it changes is made in the copy of dataframe without modifying original dataframe.

import pandas as sc
# List of Tuples
players = [('Smith', 15, 'Pune', 170000),
            ('Rana', 99, 'Mumbai', 118560),
            ('Jaydev', 51, 'Kolkata', 258741),
            ('Shikhar', 31, 'Hyderabad', 485169),
            ('Sanju', 12, 'Rajasthan', 150000),
            ('Raina', 35, 'Gujarat', 250000)
            ]
# Creation DataFrame object
playDFObj = sc.DataFrame(players, columns=['Name', 'JerseyN', 'Team', 'Salary'])
# Renaming index of dataframe as 'SL'
playDFObj.index.rename('SL', inplace=True)
print('Original Dataframe: ')
print(playDFObj)
# set column 'Name' as the index of the Dataframe
modifplayDF = playDFObj.set_index('Name')
print('Modified Dataframe of players:')
print(modifplayDF)
Output :
Original Dataframe: 
Name JerseyN Team Salary
SL 
0 Smith   15  Pune            170000
1 Rana     99  Mumbai      118560
2 Jaydev  51  Kolkata        258741
3 Shikhar 31  Hyderabad 485169
4 Sanju    12  Rajasthan   150000
5 Raina    35  Gujarat       250000

Modified Dataframe of players:
JerseyN Team Salary
Name 
Smith    15 Pune           170000
Rana     99 Mumbai      118560
Jaydev  51 Kolkata        258741
Shikhar 31 Hyderabad  485169
Sanju    12 Rajasthan    150000
Raina    35 Gujarat        250000

Converting a column of Dataframe into index without deleting the column :

In this case we will try to keep the column name and also index as ‘Name’ by passing drop argument as false.

import pandas as sc
# List of Tuples
players = [('Smith', 15, 'Pune', 170000),
            ('Rana', 99, 'Mumbai', 118560),
            ('Jaydev', 51, 'Kolkata', 258741),
            ('Shikhar', 31, 'Hyderabad', 485169),
            ('Sanju', 12, 'Rajasthan', 150000),
            ('Raina', 35, 'Gujarat', 250000)
            ]
# Creation of DataFrame object
playDFObj = sc.DataFrame(players, columns=['Name', 'JerseyN', 'Team', 'Salary'])
playDFObj.index.rename('ID', inplace=True)
# keep column name and index as 'Name'
modifplayDF = playDFObj.set_index('Name', drop=False)
print('Modified Dataframe of players:')
print(modifplayDF)
Output :
Modified Dataframe of players:
Name  JerseyN       Team  Salary
Name                                       
Smith      Smith       15       Pune  170000
Rana        Rana       99     Mumbai  118560
Jaydev    Jaydev       51    Kolkata  258741
Shikhar  Shikhar       31  Hyderabad  485169
Sanju      Sanju       12  Rajasthan  150000
Raina      Raina       35    Gujarat  250000

Appending a Dataframe column of into index to make it Multi-Index Dataframe :

In above cases the index ‘SL’ is replaced. If we want to keep it we have to pass append argument as True.

import pandas as sc
# List of Tuples
players = [('Smith', 15, 'Pune', 170000),
            ('Rana', 99, 'Mumbai', 118560),
            ('Jaydev', 51, 'Kolkata', 258741),
            ('Shikhar', 31, 'Hyderabad', 485169),
            ('Sanju', 12, 'Rajasthan', 150000),
            ('Raina', 35, 'Gujarat', 250000)
            ]
# Creation DataFrame object
playDFObj = sc.DataFrame(players, columns=['Name', 'JerseyN', 'Team', 'Salary'])
playDFObj.index.rename('SL', inplace=True)
# Making a mulit-index dataframe
modifplayDF = playDFObj.set_index('Name', append=True)
print('Modified Dataframe of players:')
print(modifplayDF)
Output :
Modified Dataframe of players:
JerseyN       Team  Salary
SL Name                              
0  Smith         15       Pune  170000
1  Rana          99     Mumbai  118560
2  Jaydev        51    Kolkata  258741
3  Shikhar       31  Hyderabad  485169
4  Sanju         12  Rajasthan  150000
5  Raina         35    Gujarat  250000

Checking for duplicates in the new index :

If we wanted to check index doesn’t contain any duplicate values after converting a column to the index by passing verify_integrity as True in set_index(). If any duplicate value found error will be raised.

import pandas as sc
# List of Tuples
players = [('Smith', 15, 'Pune', 170000),
            ('Rana', 99, 'Mumbai', 118560),
            ('Jaydev', 51, 'Kolkata', 258741),
            ('Shikhar', 31, 'Mumbai', 485169),
            ('Sanju', 12, 'Rajasthan', 150000),
            ('Raina', 35, 'Gujarat', 250000)
            ]
# Creation of DataFrame object
playDFObj = sc.DataFrame(players, columns=['Name', 'JerseyN', 'Team', 'Salary'])
# Rename index of dataframe as 'SL'
playDFObj.index.rename('SL', inplace=True)
modifplayDF = playDFObj.set_index('Team', verify_integrity=True)
print(modifplayDF)
Output :
ValueError: Index has duplicate keys

Modifying existing Dataframe by converting into index :

 We can also make changes in existing dataframe. We can implement this by assign two methods-

  1. Assign the returned dataframe object to original dataframe variable where the variable would point to updated dataframe.
  2. Passing argument inplace as True.
import pandas as sc
# List of Tuples
players = [('Smith', 15, 'Pune', 170000),
            ('Rana', 99, 'Mumbai', 118560),
            ('Jaydev', 51, 'Kolkata', 258741),
            ('Shikhar', 31, 'Hyderabad', 485169),
            ('Sanju', 12, 'Rajasthan', 150000),
            ('Raina', 35, 'Gujarat', 250000)
            ]
# Creation DataFrame object
playDFObj = sc.DataFrame(players, columns=['Name', 'JerseyN', 'Team', 'Salary'])
playDFObj.index.rename('SL', inplace=True)
playDFObj.set_index('Name', inplace=True)
print('Contenets of original dataframe :')
print(playDFObj)
Output :
Contenets of original dataframe :
JerseyN Team Salary
Name 
Smith 15 Pune 170000
Rana 99 Mumbai 118560
Jaydev 51 Kolkata 258741
Shikhar 31 Hyderabad 485169
Sanju 12 Rajasthan 150000
Raina 35 Gujarat 250000

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 – Modify a Dataframe

Pandas : Convert Dataframe column into an index using set_index() in Python Read More »

Pandas Dataframe: Get minimum values in rows or columns & their index position

Get minimum values in rows or columns & their index position

In this article we will learn to find minimum values in the rows & columns of a Dataframe and also get index position of minimum values.

DataFrame.min() :

A member function is provided by Python’s Pandas library i.e. DataFrame.min()  which can find the minimum value in a dataframe.

Syntax:- DataFrame.min(axis=None, skipna=None, level=None, numeric_only=None, **kwargs)

Some Arguments:

  1. axis- It is the axis along which minimum elements is to be searched. It is index 0 for along the rows and index 1 for along the columns.
  2. skipna- (bool) It will skip NaN or Null. It’s default is True i.e. it will be skipped if not provided.

Now, we will see the implementation of these one by one.

Get minimum values in every row & column of the Dataframe :

Get minimum values of every column :

To find the minimum element in every column of dataframe we have to only call min() member function without any argument with the DataFrame object. It will return a series with column name as index label and minimum value of each column in values.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
print('Original Dataframe :')
print(datafObj)
# Get a series that contains minimum values in each column of dataframe
minValues = datafObj.min()
print('Minimum value in each column of dataframe are : ')
print(minValues)
Output :
Original Dataframe :
a     b     c
1  10  20.0  15.0
2  35   NaN  21.0
3  18  58.0  65.0
4  11  52.0   NaN
5  98  34.0  99.0
Minimum value in each column of dataframe are :
a    10.0
b    20.0
c    15.0
dtype: float64

Get minimum values of every row :

To find the minimum values in each row in DataFrame we have to call min() member function and pass argument axis=1 with DataFrame object.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
print('Original Dataframe :')
print(datafObj)
# Get a series that contains minimum element in each rows of dataframe
minValues = datafObj.min(axis=1)
print('Minimum value in each row of dataframe are : ')
print(minValues)
Output :
Original Dataframe :
a     b     c
1  10  20.0  15.0
2  35   NaN  21.0
3  18  58.0  65.0
4  11  52.0   NaN
5  98  34.0  99.0
Minimum value in each row of dataframe are :
1    10.0
2    21.0
3    18.0
4    11.0
5    34.0
dtype: float64

In above cases we saw that it has skipped NaN, if we want we can also include NaN.

Get minimum values of every column without skipping NaN :

To get minimum value of every column without skipping NaN we have to pass skipna=False.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get a series that contains minimum elements in each column including NaN
minValues = datafObj.min(skipna=False)
print('Minimum value in each column including NaN of dataframe are : ')
print(minValues)
Output :
Minimum value in each column including NaN of dataframe are :
a    10.0
b     NaN
c     NaN
dtype: float64

Get minimum values of a single column or selected columns :

We can get minimum value of single column by calling min() member function by selecting that single column from given dataframe.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get minimum element of  a single column 'y'
minValues = datafObj['c'].min()
print("minimum value in column 'c' is : " , minValues)
Output :
minimum value in column 'c' is :  15.0

We can also get minimum value of selected columns by passing list of those columns.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get minimum value of a 'a' & 'b' columns of dataframe
minValues = datafObj[['a', 'b']].min()
print("minimum value in column 'a' & 'b' are : ")
print(minValues)
Output :
minimum value in column 'a' & 'b' are :
a    10.0
b    20.0
dtype: float64

Get row index label or position of minimum values of every column :

DataFrame.idxmin() :

We can also get the position of minimum value of DataFrame using pandas library function i.e. idxmin().

Syntax:- DataFrame.idxmin(axis=0, skipna=True)

Get row index label of minimum value in every column :

We can create a series which contains column names as index and row index labels where minimum element is found.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get the index position of minimum values in every column of dataframe
minValuesIndex = datafObj.idxmin()
print("min values of columns are at row index position :")
print(minValuesIndex)
Output :
min values of columns are at row index position :
a    1
b    1
c    1
dtype: object

Get Column names of minimum value in every row

We can also create a series which contains row index labels as index and column names as values where each row has minimum value.

import pandas as sc
import numpy as dc
# List of Tuples
matrix = [(10, 20, 15),
          (35, dc.NaN, 21),
          (18, 58, 65),
          (11, 52, dc.NaN),
          (98, 34, 99)
          ]
# Creation of DataFrame object
datafObj = sc.DataFrame(matrix, index=list('12345'), columns=list('abc'))
# Get minimum value of elements in row at respective column
minValuesIndex = datafObj.idxmin(axis=1)
print(" Minimum value in row at respective column of dataframe :")
print(minValuesIndex)
Output :
Minimum value in row at respective column of dataframe :
1    a
2    c
3    a
4    a
5    b
dtype: object

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 – Select items from a Dataframe

Pandas Dataframe: Get minimum values in rows or columns & their index position Read More »

Python : How to make a class Iterable & create Iterator Class for it ?

How to make a class Iterable & create Iterator Class for it ?

We are going to see how we can make a class iterable and also creating an iterator class for it.

What’s the need to make a Custom class Iterable ?

User defined classes are not iterable by default. To make the class objects iterable we have to make the class iterable and also create an iterator class for it.

Let’s try to iterate the class with a for loop

class School:
    """
    Contains List of Junior and senior school students
    """

    def __init__(self):
        self._juniorStudents = list()
        self._seniorStudents = list()

    def addJuniorStudents(self, members):
        self._juniorStudents += members

    def addSeniorStudents(self, members):
        self._seniorStudents += members


def main():
    # Create school class object
    school = School()
    # Add name of junior school students
    school.addJuniorStudents(["Seema", "Jenny", "Morris"
])
    # Add name of senior school students
    school.addSeniorStudents(["Ritika", "Ronnie", "Aaditya"
])
    iter(school)
        for member in school:
        print(member)
# The iter() will throw an error saying school is not iterable

The above code will throw an error as the School class is not iterable yet.

How to make your Custom Class Iterable | The Iterator Protocol :

In order to make the class iterable, we need to override the iter( ) function inside the class so that the function returns the object of the iterator class which is associated to the iterable class.

class SchoolIterator:
    """Iterator class"""

    def __init__(self, school):
        # School object reference
        self._school = school
        # index variable to keep track
        self._index = 0

    def __next__(self):
        """'Returns the next value from school object's lists"""
        if self._index < (
            len(self._school._juniorStudents) + len(self._school._seniorStudents)
        ):
            if self._index < len(
                self._school._juniorStudents
            ):  # Check if junior student members are fully iterated or not
                result = (self._school._juniorStudents[self._index], "junior")
            else:
                result = (
                    self._school._seniorStudents[
                        self._index - len(self._school._juniorStudents)
                    ],
                    "senior",
                )
            self._index += 1
            return result
        # Iteration ends
        raise StopIteration


class School:
    """
    Contains List of Junior and senior school students
    """

    def __init__(self):
        self._juniorStudents = list()
        self._seniorStudents = list()

    def addJuniorStudents(self, members):
        self._juniorStudents += members

    def addSeniorStudents(self, members):
        self._seniorStudents += members

    def __iter__(self):
        """Returns Iterator object"""
        return SchoolIterator(self)


def main():
    # Create school class object
    school = School()
    # Add name of junior school students
    school.addJuniorStudents(["Seema", "Jenny", "Morris"
])
    # Add name of senior school students
    school.addSeniorStudents(["Ritika", "Ronnie", "Aaditya"
])
    iterator = iter(school)
    print(iterator)

main()
Output :
<__main__.SchoolIterator object at 0x01BC6B10>

The __iter__( ) has been overridden in the School class which now returns the object from the schoolIterator class. And when we call iter( ) function on the school class it will call __iter__( ) function on the object.

How to create an Iterator Class :

In order to create an iterator class, we have to override the __next__( ) function so that every time we call a function, it should return the next iterable class until there are no elements. If there are no next elements, then it should raise the StopIteration.

After that we need to make the class object return the next element from the School class Object’s data member

CODE:

class SchoolIterator:
    """Iterator class"""

    def __init__(self, school):
        # School object reference
        self._school = school
        # index variable to keep track
        self._index = 0

    def __next__(self):
        """'Returns the next value from school object's lists"""
        if self._index < (
            len(self._school._juniorStudents) + len(self._school._seniorStudents)
        ):
            if self._index < len(
                self._school._juniorStudents
            ):  # Check if junior student members are fully iterated or not
                result = (self._school._juniorStudents[self._index], "junior")
            else:
                result = (
                    self._school._seniorStudents[
                        self._index - len(self._school._juniorStudents)
                    ],
                    "senior",
                )
            self._index += 1
            return result
        # Iteration ends
        raise StopIteration


class School:
    """
    Contains List of Junior and senior school students
    """

    def __init__(self):
        self._juniorStudents = list()
        self._seniorStudents = list()

    def addJuniorStudents(self, members):
        self._juniorStudents += members

    def addSeniorStudents(self, members):
        self._seniorStudents += members

    def __iter__(self):
        """Returns Iterator object"""
        return SchoolIterator(self)


def main():
    # Create school class object
    school = School()
    # Add name of junior school students
    school.addJuniorStudents(["Seema", "Jenny", "Morris"])
    # Add name of senior school students
    school.addSeniorStudents(["Ritika", "Ronnie", "Aaditya"])
    iterator = iter(school)
    print(iterator)
    while True:
        try:
            # Get next element from SchoolIterator object using iterator object
            elem = next(iterator)
            # Print the element
            print(elem)
        except StopIteration:
            break


main()
Output :
<__main__.SchoolIterator object at 0x01A96B10>
('Seema', 'junior')
('Jenny', 'junior')
('Morris', 'junior')
('Ritika', 'senior')
('Ronnie', 'senior')
('Aaditya', 'senior')

The Working

The iter( ) function calls the overridden __iter__( ) function on the school objects, which would return, the SchoolIterator object. Upon calling the next( ) function, it would call our overridden function  __next__( ) internally. The _index variable is being used here to keep track of the iterated elements. So every time we call the function it iterates the objects and in the need it raises the StopIteration.

class SchoolIterator:
    """Iterator class"""

    def __init__(self, school):
        # School object reference
        self._school = school
        # index variable to keep track
        self._index = 0

    def __next__(self):
        """'Returns the next value from school object's lists"""
        if self._index < (
            len(self._school._juniorStudents) + len(self._school._seniorStudents)
        ):
            if self._index < len(
                self._school._juniorStudents
            ):  # Check if junior student members are fully iterated or not
                result = (self._school._juniorStudents[self._index], "junior")
            else:
                result = (
                    self._school._seniorStudents[
                        self._index - len(self._school._juniorStudents)
                    ],
                    "senior",
                )
            self._index += 1
            return result
        # Iteration ends
        raise StopIteration


class School:
    """
    Contains List of Junior and senior school students
    """

    def __init__(self):
        self._juniorStudents = list()
        self._seniorStudents = list()

    def addJuniorStudents(self, members):
        self._juniorStudents += members

    def addSeniorStudents(self, members):
        self._seniorStudents += members

    def __iter__(self):
        """Returns Iterator object"""
        return SchoolIterator(self)


def main():
    # Create school class object
    school = School()
    # Add name of junior school students
    school.addJuniorStudents(["Seema", "Jenny", "Morris"])
    # Add name of senior school students
    school.addSeniorStudents(["Ritika", "Ronnie", "Aaditya"])
    iterator = iter(school)
#Using for loop
    print(iterator)
    for member in school:
        print(member)


main()
Output :

<__main__.SchoolIterator object at 0x7fefbe6f34f0>
('Seema', 'junior')
('Jenny', 'junior')
('Morris', 'junior')
('Ritika', 'senior')
('Ronnie', 'senior')
('Aaditya', 'senior')
>

Python : How to make a class Iterable & create Iterator Class for it ? Read More »

numpy.zeros() & numpy.ones() | Create a numpy array of zeros or ones

Create a 1D / 2D Numpy Arrays of zeros or ones

We are going to how we can create variou types of numpy arrays.

numpy.zeros( ) :

The numpy module in python makes it able to create a numpy array all initialized with 0’s.

Syntax: numpy.zeros(shape, dtype=, order=)

Parameters :

  1. shape : The shape of the numpy array.(single Int or a sequence)
  2. dtype : It is an optional parameter that takes the data type of elements.(default value is float32)
  3. order : It is also an optional parameter which defines the order in which the array will be stored(‘C’ for column-major which is also the default value and ‘F’ for row-major)

Flattened numpy array filled with all zeros :

Below code is the implementation for that.

import numpy as np

#Creating a numpy array containing all 0's
zeroArray = np.zeros(5)
print("The array contents are : ", zeroArray)
Output : 
The array contents are :  [0. 0. 0. 0. 0.]

Creating a 2D numpy array with 5 rows & 6 columns, filled with 0’s :

To create an array with 5 rows and 6 columns filled with all 0’s, we need to pass 5 and 6 as parameters into the function.

Below code is the implementation for that.

import numpy as np

# Creating a 5X6 numpy array containing all 0's
zeroArray = np.zeros((5, 6))
print("The array contents are : ", zeroArray)
Output :
The array contents are :  [[0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]
 [0. 0. 0. 0. 0. 0.]]

It created a zero numpy array of 5X6 size for us.

numpy.ones( ) :

Just like the numpy.zeros( ), numpy.ones( ) is used to initialize the array elements to 1. It has same syntax.

Syntax - numpy.ones(shape, dtype=float, order='C')

Creating a flattened numpy array filled with all Ones :

Below code is the implementation for that.

import numpy as np

# Creating a numpy array containing all 1's
oneArray = np.ones(5)
print("The array contents are : ", oneArray)
Output :
The array contents are :  [1. 1. 1. 1. 1.]

Creating a 2D numpy array with 3 rows & 4 columns, filled with 1’s :

To create a 2D numpy array with 3 rows and 4 columns filled with 1’s, we have to pass (3,4) into the function.

Below code is the implementation for that.

import numpy as np

# Creating a 3X4 numpy array containing all 1's
oneArray = np.ones((3, 4))
print("The array contents are : ", oneArray)
print("Data Type of elements in  Array : ", oneArray.dtype)
Output :
The array contents are :  [[1. 1. 1. 1.]
 [1. 1. 1. 1.]
 [1. 1. 1. 1.]]
Data Type of elements in  Array :  float64

Let’s see how we can set the datatype to integer.

import numpy as np

# Creating a 3X4 numpy array containing all 1's int64 datatype
oneArray = np.ones((3, 4), dtype=np.int64)
print("The array contents are : ", oneArray)
print("Data Type of elements in  Array : ", oneArray.dtype)
Output :
The array contents are :  [[1 1 1 1]
 [1 1 1 1]
 [1 1 1 1]]
Data Type of elements in  Array :  int64

 

numpy.zeros() & numpy.ones() | Create a numpy array of zeros or ones Read More »

Convert PDF to TXT file using Python

Convert PDF to TXT file using Python

You must all be aware of what PDFs are. They are, in fact, one of the most essential and extensively utilized forms of digital media. PDF is an abbreviation for Portable Document Format. It has the.pdf extension. It is used to reliably exhibit and share documents, regardless of software, hardware, or operating system.

Text Extraction from a PDF File
The Python module PyPDF can be used to achieve what we want (text extraction), but it can also do more. This software can also produce, decrypt, and merge PDF files.

Why pdf to txt is needed?

Before we get into the meat of this post, I’ll go over some scenarios in which this type of PDF extraction is required.

One example is that you are using a job portal where people used to upload their CV in PDF format. And when

recruiters are looking for specific keywords, such as Hadoop developers, big data developers, python developers,

java developers, and so on. As a result, the keyword will be matched with the skills that you have specified in your

resume. This is another processing step in which they extract data from your PDF document and match it with the

keyword that the recruiter is looking for, and then they simply give you your name, email, or other information.

As a result, this is the use case.

Python has various libraries for PDF extraction, but we’ll look at the PyPDF2 module here. So, let’s look at how to

extract text from a PDF file using this module.

Convert PDF to TXT file using Python

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

1)PyPDF2 module

PyPDF2 is a Pure-Python package designed as a PDF toolkit. It is capable of:

obtaining document information (title, author, etc)

separating documents page by page

merging documents page by page

cropping pages

merging several pages into a single page

encoding and decrypting PDF files and more!
So, now we’ll look at how to extract text from a PDF file using the PyPDF2 module. In your Python IDE, enter the following code (check best python IDEs).

2)Creating a Pdf file

  • Make a new document in Word.
  • Fill up the word document with whatever material you choose.
  • Now, Go to File > Print > Save.
  • Remember to save your pdf file in the same folder as your Python script.
  • Your.pdf file has now been created and saved, and it will be converted to a.txt file later.

3)Install PyPDF2

First, we’ll add an external module called PyPDF2.

The PyPDF2 package is a pure Python pdf library that may be used to divide, merge, crop, and alter PDF files. PyPDF2 may also be used to add data, viewing choices, and passwords to PDFs, according to the PyPDF2 website.

To install the PyPDF2 package, start a command prompt in Windows and use the pip command to install  PyPDF2

4)Creating and opening new Python Project

Open the Python IDLE and hit the ctrl + N keys. This launches your text editor.

You are free to use any other text editor of your choosing.

You should save the file as your pdf file_name.py.

Save this.py file in the same folder as your pdf.

5)Implementation

Below is the implementation:

import PyPDF2

# The opening procedure for a file object variable will be rb
pdffile = open(r'C:\Users\Vikram\Desktop\samplepdf.pdf', 'rb')

# create a variable called reader that will read the pdf file
pdfReader = PyPDF2.PdfFileReader(pdffile)

# The number of pages in this pdf file will be saved.
num = pdfReader.numPages

# create a variable that will select the selected number of pages
pageobj = pdfReader.getPage(num+1)

resulttext = pageobj.extractText()

newfile = open(
    r"C:\Users\Vikram\Desktop\Calender\\sample.txt", "a")
newfile.writelines(resulttext)

Output:

Python Programming Online
Tutorial | Free Beginners’ Guide on
Python Programming Language
Do you Love to Program in Python Language? Are you completely new to the Phyton programming language? Then, refer to this ultimate guide on Python Programming and become the top programmer. For detailed information such as What is Python? Why we use it? Tips to Learn Python Programming Language, Applications for Python dive into this article.

6)Explanation

We start by creating a Python file object and then opening the PDF file in “read binary (rb)” mode.
The PdfFileReader object is then created, which will read the file opened in the previous step.
The number of pages in the file is stored in a variable.
The final step saves the detected lines from the PDF to a text file you designate.
Related Programs:

Convert PDF to TXT file using Python Read More »

Program to Print Binary Representation of a Number

Python Program to Print Binary Representation of a Number

Binary Representation:

Binary (or base-2) is a numeral system with only two digits — 0 and 1. Computers use binary to store data and execute calculations, which means they only use zeros and ones. In Boolean logic, a single binary digit can only represent True (1) or False (0). Any integer, in fact, can be represented in binary.

Given a decimal number the task is to convert the given decimal to binary

Examples:

Example1:

Input:

given number =200

Output:

The binary representation of the given number 200  : 
11001000

Example2:

Input:

given number =1

Output:

The binary representation of the given number 1: 
1

Example3:

Input:

given number =32

Output:

The binary representation of the given number 32  : 
100000

Python Program to Print Binary Representation of a Number

There are several ways to print the binary representation of a given decimal number some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1:Using recursive function

To find a binary equivalent, divide the decimal number recursively by the value 2 until the decimal number hits zero. The remaining must be noted after each division. The binary equivalent of the decimal number is obtained by reversing the remaining values.

Algorithm:

  1. Get the given decimal number from the user or static input.
  2. If the input is larger than zero, divide it by 2 and record the remainder.
  3. Step 2 should be repeated until the decimal number reaches zero.
  4. The residual values should be printed.
  5. End of program

Below is the implementation:

def decitoBin(numb):
    # checking if the given number is greater than 1
    if numb > 1:
        # if it is greater than 1 then use recursive approach by dividing number by 2
        decitoBin(numb // 2)
    # printing the binary representation of the given number
    print(numb % 2, end='')


# Driver code
given_numb = 200
# passing given number to decitoBin function to print binary representation of the givennumb
print("The binary representation of the given number", given_numb, " : ")
decitoBin(given_numb)

Output:

The binary representation of the given number 200  : 
11001000

Method #2:Using while loop

Approach:

  • First we take a empty string say binstr.
  • We use while loop .
  • We will iterate till the number is greater than 0 (Condition of while statement)
  • We will get the last check bit whether it is set bit or not using % operator
  • Convert the set bit to string using str() function
  • Concatenate this bit(can be 1 or 0 ) to the binstr.
  • Reverse this binstr using slicing
  • Print the binary representation of the given number that is print binstr.

Below is the implementation:

def decitoBin(numb):
    # checking if the given number is greater than 1
    if numb > 1:
      # taking a empty string
        binastring = ""
        # looping till number greater than 0 using while loop
        while(numb > 0):
            # We will get the last check bit whether it is set bit or not using % operator
            checkbit = numb % 2
            # converting this checkbit to string using str() function
            checkbit = str(checkbit)
            # Concatenate this bit(can be 1 or 0 ) to the binstr.
            binastring = binastring+checkbit
            # divide the number by 2
            numb = numb//2
    # reverse the binary string
    binastring = binastring[::-1]
    # return the resultant binary string
    return binastring


# Driver code
given_numb = 200
# passing given number to decitoBin function to print binary representation of the givennumb
print("The binary representation of the given number", given_numb, " : ")
print(decitoBin(given_numb))

Output:

The binary representation of the given number 200  : 
11001000

Method #3:Using Built in Python function bin()

Approach:

  • We will use bin() function to convert the given decimal number to binary representation.
  • It will return the result in the form of  0bXXXXXXXXX where XXXXXXXX is binary representation.
  • To remove that 0b characters from the string we use two methods
  • Print the resultant binary representation.

i)Using replace function

We will replace the 0b in the binary string with empty string.

Below is the implementation:

def decitoBin(numb):
  # converting it to binary representation using bin() function
    binNumb = bin(numb)
    # replacing '0b' using replace function and replacing it with empty string
    binNumb = binNumb.replace('0b', '')
  # return the binary representation of the given number
    return binNumb


# Driver code
given_numb = 200
# passing given number to decitoBin function to print binary representation of the givennumb
print("The binary representation of the given number", given_numb, " : ")
print(decitoBin(given_numb))

Output:

The binary representation of the given number 200  : 
11001000

The binary representation of the given decimal number will be printed.

ii)Using slicing

We will slice from 2 index to last index of the result returned by binary string

Below is the implementation:

def decitoBin(numb):
  # converting it to binary representation using bin() function
    binNumb = bin(numb)
    # We will slice from 2 index to last index of the result returned by binary string
    binNumb = binNumb[2:]
  # return the binary representation of the given number
    return binNumb


# Driver code
given_numb = 200
# passing given number to decitoBin function to print binary representation of the givennumb
print("The binary representation of the given number", given_numb, " : ")
print(decitoBin(given_numb))

Output:

The binary representation of the given number 200  : 
11001000

The binary representation of the given decimal number will be printed.

Related Programs:

Python Program to Print Binary Representation of a Number Read More »

Program to Check if a String is a Pangram or Not

Python Program to Check if a String is a Pangram or Not

Strings in Python:

A Python string is an ordered collection of characters used to express and store text-based data. Strings are saved in an adjacent memory area as individual characters. It is accessible in both directions: forward and backward. Characters are merely symbols. Strings are immutable Data Types in Python, which means they cannot be modified once they are formed.

Pangram:

If a sentence or string contains all 26 letters of the English alphabet at least once, it is considered to be a pangram.

Examples:

Example1:

Input:

given string ="Helloabcdfegjilknmporqstvuxwzy"

Output:

The given string helloabcdfegjilknmporqstvuxwzy is a pangram

Example2:

Input:

given string ="hellothisisbtechgeeks"

Output:

The given string hellothisisbtechgeeks is not a pangram

Python Program to Check if a String is a Pangram or Not

There are several ways to check if the given string is pangram or not some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1:Naive Approach

Approach:

  • Scan the given string or provide static input.
  • Use the lower() method to convert this string to lowercase.
  • The brute force method is to take a string that contains all of the letters of the English alphabet.
  • Traverse through all of the alphabet’s characters string
  • Check to see if this character appears in the given string.
  • If it isn’t present, return False.
  • Return True at the end of the loop (which implies it is pangram)

Below is the implementation:

# Python Program to Check if a String is a Pangram or Not
def checkPangramString(string):

    # creating a new string (alphabet string which stores all the alphabets of the english language
    AlphabetString = 'abcdefghijklmnopqrstuvwxyz'

    # Traverse through the alphabets string
    for char in AlphabetString:
        # Check if this character is present in given string .
        if char not in string.lower():
            # if yes then this character is not available hence return False
            return False
    # After the end of loop return True (which implies it is pangram)
    return True


# given string
string = "Helloabcdfegjilknmporqstvuxwzy"
# converting the given string into lower case
string = string.lower()
# passing this string to checkPangramString function which returns true
# if the given string is pangram else it will return false

if checkPangramString(string):
    print("The given string", string, "is a pangram")
else:
    print("The given string", string, "is not a pangram")

Output:

The given string helloabcdfegjilknmporqstvuxwzy is a pangram

Method #2:Using set() method

The following program can be easily implemented in Python by using the set() method.

Approach:

  • Scan the given string or provide static input.
  • Use the lower() method to convert this string to lowercase.
  • Use the set() function to convert this string to a set.
  • Determine the length of the set.
  • If the length is 26, it is a pangram (since the English language has only 26 alphabets).
  • Otherwise, it is not a pangram.

Below is the implementation:

# Python Program to Check if a String is a Pangram or Not
def checkPangramString(string):

    # converting given string to set using set() function
    setString = set(string)
    # calculate the length of the set
    length = len(setString)
    # If the length is 26, it is a pangram so return true
    if(length == 26):
        return True
    else:
        return False


# given string
string = "Helloabcdfegjilknmporqstvuxwzy"
# converting the given string into lower case
string = string.lower()
# passing this string to checkPangramString function which returns true
# if the given string is pangram else it will return false

if checkPangramString(string):
    print("The given string", string, "is a pangram")
else:
    print("The given string", string, "is not a pangram")

Output:

The given string helloabcdfegjilknmporqstvuxwzy is a pangram

Note:

This method is only applicable if the given string contains alphabets.

Method #3:Using Counter() function (Hashing)

The following program can be easily implemented in Python by using the counter() function

Approach:

  • Scan the given string or provide static input.
  • Use the lower() method to convert this string to lowercase.
  • Calculate the frequency of all characters in the given string using Counter() method
  • Calculate the length of the Counter dictionary.
  • If the length is 26, it is a pangram (since the English language has only 26 alphabets).
  • Otherwise, it is not a pangram.

Below is the implementation:

# Python Program to Check if a String is a Pangram or Not
# importing counter from collections
from collections import Counter


def checkPangramString(string):

    # Calculate the frequency of all characters in the given string
    # using Counter() method
    frequ = Counter(string)
    # calculate the length of the frequency dictionary which is
    # returned from counter() function
    length = len(frequ)
    # If the length is 26, it is a pangram so return true
    if(length == 26):
        return True
    else:
        return False


# given string
string = "Helloabcdfegjilknmporqstvuxwzy"
# converting the given string into lower case
string = string.lower()
# passing this string to checkPangramString function which returns true
# if the given string is pangram else it will return false

if checkPangramString(string):
    print("The given string", string, "is a pangram")
else:
    print("The given string", string, "is not a pangram")

Output:

The given string helloabcdfegjilknmporqstvuxwzy is a pangram

Note:

This method is only applicable if the given string contains alphabets.

Related Programs:

Python Program to Check if a String is a Pangram or Not Read More »

Program to Check Whether the given Number is Strong Number or Not

Python Program to Check Whether the given Number is Strong Number or Not

Strong number:

A Strong number is a special number in which the total of all digit factorials equals the number itself.

Ex: 145 the sum of factorial of digits = 1 ! + 4 ! +5 ! = 1 + 24 +125

To determine whether a given number is strong or not. We take each digit from the supplied number and calculate its factorial , we will do this for each digit of the number.

We do the sum of factorials once we have the factorial of all digits. If the total equals the supplied number, the given number is strong; otherwise, it is not.

Given a number the task is to check whether the given number is strong number or not.

Examples:

Example1:

Input:

given number = 145

Output:

The given number 145 is strong number

Example2:

Input:

given number = 176

Output:

The given number 176 is not a strong number

Python Program to Check Whether the given Number is Strong Number or Not

There are several ways to check whether the given number is strong number or not some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

To determine if a number is a strong number or not, divide it into distinct units by each of its digits. The factorial of each of the digits is then computed. Then we will add the values received after the factorial operation for each of the digits. Finally, it must be determined whether this sum equals the supplied number. If the answer is yes, the number is a strong number.

Method #1:Using While loop and writing original factorial function

Approach:

  1. Obtain the desired number as input to determine if it is a strong number, or provide the input number as static.
  2. Let’s create a variable called N to hold this input number.
  3. Put the value of N in a temporary variable called tempNum.
  4. Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
  5. The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
  6. last_Digit  factorial must be kept in a variable, say factNum.
  7. When the factorial of the last digit is found, it should be added to the totalSum = totalSum+ factNum
  8. Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is  N = N /10
  9. Steps 4–7 should be repeated until N > 0.

Below is the implementation:

# python program to cheeck whether the given numner is strong number or not
def checkStrongNumb(givenNumb):
    # Taking a variable totalSum and initializing it with 0
    totalSum = 0
    # Take a temporary variable and store the given number (givenNumb)
    # in the temporary variable tempNum
    tempNum = givenNumb
    # using while to extract digit by digit of the given number
    while(givenNumb):
        s = 1
        factNum = 1
        # Getting the last digit of the given number
        remainder = givenNumb % 10
        # calculating the factorial of the digit(extracted by remainder variable)
        while(s <= remainder):
            factNum = factNum * s
            s = s + 1
        # Adding the factorial to the totalSum
        totalSum = totalSum + factNum
        # Dividing the given number by 10
        givenNumb = givenNumb//10
    # checking if the totalSum is equal to the given number
    # if it is true then it is strong number then return true
    if(totalSum == tempNum):
        return True
    # if nothing is returned then it is not a strong number so return False
    return False


# Given number
given_numb = 145
# passing the givennumber to checkStrongNumb to check whether it is strong number or not
if(checkStrongNumb(given_numb)):
    print("The given number", given_numb, "is strong number")
else:
    print("The given number", given_numb, "is not a strong number")

Output:

The given number 145 is strong number

Method #2:Using While loop and factorial() function

We use the factorial() function to do the above problem quickly

Approach:

  1. Obtain the desired number as input to determine if it is a strong number, or provide the input number as static.
  2. Let’s create a variable called N to hold this input number.
  3. Put the value of N in a temporary variable called tempNum.
  4. Set a variable, say totalSum to zero. This will save the factorial sum of each of N’s digits.
  5. The number’s final digit must be saved in a variable, such as last_Digit = N % 10.
  6. Calculate the factorial of the last_Digit using math.factorial() function
  7. When the factorial of the last digit is found, it should be added to the totalSum = totalSum+ factNum
  8. Following each factorial operation, the number must be reduced in terms of units by dividing it by ten that is  N = N /10
  9. Steps 4–7 should be repeated until N > 0.

Below is the implementation:

# importing math module
import math
# python program to cheeck whether the given numner is strong number or not


def checkStrongNumb(givenNumb):
    # Taking a variable totalSum and initializing it with 0
    totalSum = 0
    # Take a temporary variable and store the given number (givenNumb)
    # in the temporary variable tempNum
    tempNum = givenNumb
    # using while to extract digit by digit of the given number
    while(givenNumb):
        s = 1
        factNum = 1
        # Getting the last digit of the given number
        remainder = givenNumb % 10
        # calculating the factorial of the digit(extracted by remainder variable)
        # using math.fatorial function
        factNum = math.factorial(remainder)
        # Adding the factorial to the totalSum
        totalSum = totalSum + factNum
        # Dividing the given number by 10
        givenNumb = givenNumb//10
    # checking if the totalSum is equal to the given number
    # if it is true then it is strong number then return true
    if(totalSum == tempNum):
        return True
    # if nothing is returned then it is not a strong number so return False
    return False


# Given number
given_numb = 145
# passing the givennumber to checkStrongNumb to check whether it is strong number or not
if(checkStrongNumb(given_numb)):
    print("The given number", given_numb, "is strong number")
else:
    print("The given number", given_numb, "is not a strong number")

Output:

The given number 145 is strong number

Related Programs:

Python Program to Check Whether the given Number is Strong Number or Not Read More »

Program to Check Whether the given Number is Perfect Number or Not in Python

Python Program to Check Whether the given Number is Perfect Number or Not

Perfect Number in python:

If the sum of a number’s appropriate divisors (excluding the number itself) equals the number, the number is said to be the perfect number.

Consider the following example: appropriate divisors of 6 are 1, 2, 3. Because the sum of these divisors equals 6 (1+2+3=6), 6 is considered a perfect number. When we consider another number, such as 12, the proper divisors of 12 are 1, 2, 3, 4, and 6. Now, because the sum of these divisors does not equal 12, 12 is not a perfect number.

Python programming is simpler and more enjoyable than programming in other languages due to its simplified syntax and superior readability. Now that we understand the concept of a perfect number, let’s construct a Python program to determine whether or not a number is a perfect number. Let’s write some Python code to see if the given user input is a perfect number or not, and have some fun with Python coding.

Examples:

Example1:

Input:

given number = 6

Output:

The given number 6 is perfect number

Example2:

Input:

given number = 179

Output:

The given number 179 is not a perfect number

Python Program to Check Whether the given Number is Perfect Number or Not

There are several ways to check whether the given number is perfect number or not some of them are:

Drive into Python Programming Examples and explore more instances related to python concepts so that you can become proficient in generating programs in Python Programming Language.

Method #1: Iterating from 2 to N-1

A simple technique for determining a perfect number is to loop from 2 to given_number-1, keep track of the sum of the number’s proper divisors, and see if the sum equals the number.

Below is the implementation:

# python program to cheeck whether the given numner is perfect number or not

# function which returns true if the given number is
# perfect number else it will return False


def checkPerfectNumb(givenNumb):
    # Taking a variable totalSum and initializing it with 1
    totalSum = 1
    # Iterating from 2 to n-1
    for i in range(2, givenNumb):
        # if the iterator value is divides the number then add the given number to totalSum
        if givenNumb % i == 0:
            totalSum += i

    # if the totalSum is equal to the given number
    # then it is perfect number else it is not perfect number

    if(totalSum == givenNumb):
        # if it is true then it is perfect number then return true
        return True
    # if nothing is returned then it is not a perfect number so return False
    return False


# Given number
given_numb = 6
# passing the givennumber to checkPerfectNumb to check whether it is perfect number or not
if(checkPerfectNumb(given_numb)):
    print("The given number", given_numb, "is perfect number")
else:
    print("The given number", given_numb, "is not a perfect number")

Output:

The given number 6 is perfect number

We must determine whether n is a perfect number. It is worth noting that we are initializing the totalSum with 1 because 1 is a valid divisor for all integers (except zero), allowing us to skip an iteration in the loop and start from 2.

If it is a proper divisor, we loop over 2 to number-1 and add the integers to totalSum. Finally, when we exit the loop, we check to see if the sum obtained is equal to the number.

It requires O(n) Time Complexity.

Method #2:Iterating from 2 to N/2

After running the preceding program through its tests, we may think it would be possible to optimize it. But, without modifying the technique, we can reduce the number of iterations to number/2. We came to the conclusion that a number cannot have a suitable divisor bigger than number/2.

Below is the implementation:

# python program to cheeck whether the given numner is perfect number or not

# function which returns true if the given number is
# perfect number else it will return False


def checkPerfectNumb(givenNumb):
    # Taking a variable totalSum and initializing it with 1
    totalSum = 1
    # Iterating from 2 to given_number//2 +1
    for i in range(2, givenNumb//2 + 1):
        # if the iterator value is divides the number then add the given number to totalSum
        if givenNumb % i == 0:
            totalSum += i

    # if the totalSum is equal to the given number
    # then it is perfect number else it is not perfect number

    if(totalSum == givenNumb):
        # if it is true then it is perfect number then return true
        return True
    # if nothing is returned then it is not a perfect number so return False
    return False


# Given number
given_numb = 6
# passing the givennumber to checkPerfectNumb to check whether it is perfect number or not
if(checkPerfectNumb(given_numb)):
    print("The given number", given_numb, "is perfect number")
else:
    print("The given number", given_numb, "is not a perfect number")

Output:

The given number 6 is perfect number

The preceding excerpt is nearly identical to the previous one, with the exception of looping till number/2. It’s worth noting that we’re doing an integer division instead of converting it to a float type, and we’re looping until n/2+1 because the last integer in the range is ignored by the Python loop.

It requires O(n) Time Complexity.

Method #3: Efficient Approach (Iterating till Square root of N)

An Efficient Solution is to go through the numbers till you get to the square root of n. If a number i divides n, then sum both i and n/i.

Below is the implementation:

# python program to cheeck whether the given numner is perfect number or not

# function which returns true if the given number is
# perfect number else it will return False


def checkPerfectNumb(givenNumb):
    # Taking a variable totalSum and initializing it with 1
    totalSum = 1
    k = 2
    while k * k <= givenNumb:
        # if the iterator value is divides the number then add the given number to totalSum
        if givenNumb % k == 0:
            totalSum = totalSum + k + givenNumb/k
        k += 1

    # if the totalSum is equal to the given number
    # then it is perfect number else it is not perfect number

    if(totalSum == givenNumb):
        # if it is true then it is perfect number then return true
        return True
    # if nothing is returned then it is not a perfect number so return False
    return False


# Given number
given_numb = 6
# passing the givennumber to checkPerfectNumb to check whether it is perfect number or not
if(checkPerfectNumb(given_numb)):
    print("The given number", given_numb, "is perfect number")
else:
    print("The given number", given_numb, "is not a perfect number")

Output:

The given number 6 is perfect number

This is the efficient approach to do the same problem quickly compared to first two methods.

It requires O(Sqrt(n)) Time Complexity.

Related Programs:

Python Program to Check Whether the given Number is Perfect Number or Not Read More »