  Apply function to every row in Pandas DataFrame

NumPy | Python Methods and Functions

You can use the apply () function to apply the function to each line in a given data frame. Let's see how we can accomplish this task.

Example # 1:

 # Import pandas package import pandas as pd   # Function to add def add (a, b, c): return a + b + c   def main ():    # create with C++ # three fields each data = { 'A' : [ 1 , 2 , 3 ],  'B' : [ 4 , 5 , 6 ],  'C' : [ 7 , 8 , 9 ]}      # Convert dictionary to DataFrame df = pd .DataFrame (data) print ( "Original DataFrame:" , df)     df [ ' add' ] = df. apply ( lambda row: add (row [ 'A' ], row [ 'B' ], row [ ' C' ]), axis = 1 )   print ( 'After Applying Function:' ) # print new data frame   print (df)   if __ name__ = = '__mai n__' : main ()

Output: Example # 2:

You can also use numpy function as parameters for data frame.

 import pandas as pd import numpy as np   def main ():   # create a dictionary with # five fields each   data = {   ' A ' : [ 1 , 2 , 3 ],  'B' : [ 4 , 5 , 6 ],  'C' : [ 7 , 8 , 9 ]}   # Convert dictionary to DataFrame df = pd.DataFrame (data) print ( "Original DataFrame:" , df)   # applying the function to each row in the dataframe # and saving the result in a new column df [ 'add' ] = df. apply (np. sum , axis = 1 )   print ( 'After Applying Function:' ) # print new data frame print (df)   if __ name__ = = '__main__' :   main ( )

Output: Example # 3: data normalization

 # Pandas package import import pandas as pd   def normalize (x, y):   x_new = ((x - np.mean ([ x, y])) / ( max (x, y) - min (x, y)))   # print (x_new) return x_new   def main ():   # create a dictionary with three fields each data = { 'X' : [ 1 , 2 , 3 ],  'Y' : [ 45 , 65 , 89 ]}   # Convert dictionary to DataFrame df = pd.DataFrame (data) print ( "Original DataFrame:" , df)   df [ 'X' ] = df. apply ( lambda row: normalize (row [ 'X' ], row [ 'Y' ]), axis = 1 )   print ( ' Normalized: ' ) print (df)    if __ name__ = = '__main__' : main ()

Output: Example # 4: Create Range

 import pandas as pd import numpy as np    pd.options.mode.chained_assignment = None    # Function to generate a range def gene rate_range (n):   # print a range, for example: # in 67 out 60 -70 n = int (n)   lower_limit = n / / 10 * 10 upper_limit = lower_limit + 10   return str ( str (lower_limit ) + '-' + str (upper_limit))   def replace (row): for i, item in enumerate (row):   # update string value r ow [i] = generate_range (item) return row      def main (): # create a dictionary with # three fields each   data = { 'A' : [ 0 , 2 , 3 ],    ' B' : [ 4 , 15 , 6 ],  'C' : [ 47 , 8 , 19 ]}   # Convert dictionary to DataFrame df = pd.DataFrame (data)     print ( 'Before applying function:' ) print (df)    # apply function to each line in # dataframe and saving the result in a new column df = df. apply ( lambda row: replace (row))     print ( 'After Applying Function:' )   # print new data frame print (df)   if __ name__ = = '__main__' :   main ( )

Output: 