{"id":4168,"date":"2021-04-27T19:34:40","date_gmt":"2021-04-27T14:04:40","guid":{"rendered":"https:\/\/python-programs.com\/?p=4168"},"modified":"2021-11-22T18:53:45","modified_gmt":"2021-11-22T13:23:45","slug":"pandas-apply-apply-a-function-to-each-row-column-in-dataframe","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-apply-apply-a-function-to-each-row-column-in-dataframe\/","title":{"rendered":"pandas.apply(): Apply a function to each row\/column in Dataframe"},"content":{"rendered":"

How to apply a function\u00a0 to each row or column in Dataframe in Python.<\/h3>\n

To apply a function to each row or column data in a warframe be it lambda, user-defined or a numpy function we have to use a function from Python\u2019s Pandas library.\u00a0 The function belongs to the dataframe class .<\/p>\n

Syntax-<\/u><\/pre>\n

DataFrame.apply(func, axis=0, broadcast=None, raw=False, reduce=None, result_type=None, args=(), **kwds)<\/em><\/p>\n

Arguments :<\/strong><\/p>\n

    \n
  1. Func :<\/strong> It is the function that is to be applied to the rows\/columns. It takes series as arguments and also returns series<\/li>\n
  2. Axis :<\/strong> Axis is the axis in which the function is applied to the rows\/columns. (default value is 0. If 1 means it applies to all rows, If 0 means it applies to all columns)<\/li>\n
  3. Args :<\/strong> All the arguments passed in a list of tuples.<\/li>\n<\/ol>\n

    Apply a lambda function to each row or each column in Dataframe :<\/strong><\/h3>\n

    Let us consider a lambda function<\/p>\n

    lambda x : x + 10<\/em><\/p>\n

    Apply a lambda function to each column :<\/strong><\/h4>\n

    To apply the function to each column we just have to pass the lambda function as argument in the Dataframe.apply( )<\/code> function.<\/p>\n

    #Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#list of tuples\r\nmatrix = [(222, 34, 23),\r\n         (333, 31, 11),\r\n         (444, 16, 21),\r\n         (555, 32, 22),\r\n         (666, 33, 27),\r\n         (777, 35, 11)\r\n         ]\r\n#creating an object from Dataframe class\r\ndfObj = pd.DataFrame(matrix, columns=list('abc'))\r\n#Passing only the lambda function into the Dataframe function so that it gets applied to columns only\r\nmodMatrix = dfObj.apply(lambda x : x + 10)\r\nprint(\"After applying the lambda function to each column in dataframe\")\r\nprint(modMatrix)\r\n<\/pre>\n
    Output :\r\n\r\nAfter applying the lambda function to each column in dataframe\r\n\r\n \u00a0\u00a0\u00a0 a\u00a0\u00a0    b\u00a0\u00a0 c\r\n0\u00a0 232\u00a0 44\u00a0 33\r\n1\u00a0 343\u00a0 41\u00a0 21\r\n2\u00a0 454\u00a0 26\u00a0 31\r\n3\u00a0 565\u00a0 42\u00a0 32\r\n4\u00a0 676\u00a0 43\u00a0 37\r\n5\u00a0 787\u00a0 45\u00a0 21<\/pre>\n

    Apply a lambda function to each row :<\/strong><\/h4>\n

    To apply the function to each row we just have to add axis=1 <\/em>and pass it to the lambda function with the lambda function in the Dataframe.apply( )<\/code><\/em><\/strong> function like we did in the column.<\/p>\n

    #Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#list of tuples\r\nmatrix = [(222, 34, 23),\r\n         (333, 31, 11),\r\n         (444, 16, 21),\r\n         (555, 32, 22),\r\n         (666, 33, 27),\r\n         (777, 35, 11)\r\n         ]\r\n#creating an object from Dataframe class\r\ndfObj = pd.DataFrame(matrix, columns=list('abc'))\r\n#Passing only the lambda function into the Dataframe function so that it gets applied to columns only\r\nmodMatrix = dfObj.apply(lambda x : x + 10)\r\nprint(\"After applying the lambda function to each row in dataframe\")\r\nprint(modMatrix)\r\n<\/pre>\n
    Output :\r\nAfter applying the lambda function to each row in dataframe\r\n \u00a0\u00a0\u00a0 a\u00a0\u00a0    b\u00a0\u00a0  c\r\n0\u00a0 227\u00a0 39\u00a0 28\r\n1\u00a0 338\u00a0 36\u00a0 16\r\n2\u00a0 449\u00a0 21\u00a0 26\r\n3\u00a0 560\u00a0 37\u00a0 27\r\n4\u00a0 671\u00a0 38\u00a0 32\r\n5\u00a0 782\u00a0 40\u00a0 16<\/pre>\n

    Apply a User Defined function with or without arguments to each row or column of a Dataframe :<\/strong><\/h3>\n

    For this let us consider a user-defined function that multiplies the values by 2<\/p>\n

    def doubleData(x):<\/em><\/p>\n

    <\/pre>\n

    return x * 2<\/em><\/p>\n

    Apply a user-defined function to each column :<\/strong><\/h4>\n

    Like we applied the lambda function to each column, similarly we will only pass the function here.<\/p>\n

    #Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n# Multiply given value by 2 and returns\r\ndef doubleData(x):\r\n   return x * 2\r\n#list of tuples\r\nmatrix = [(222, 34, 23),\r\n         (333, 31, 11),\r\n         (444, 16, 21),\r\n         (555, 32, 22),\r\n         (666, 33, 27),\r\n         (777, 35, 11)\r\n         ]\r\n#creating an object from Dataframe class\r\ndfObj = pd.DataFrame(matrix, columns=list('abc'))\r\n#Applyin the user defined function doubleData to columns only\r\nmodMatrix = dfObj.apply(doubleData)\r\nprint(\"After applying the user-defined function to each column in dataframe\")\r\nprint(modMatrix)\r\n<\/pre>\n
    Output :\r\nAfter applying the user-defined function to each column in dataframe\r\n \u00a0\u00a0\u00a0\u00a0 a\u00a0\u00a0    b\u00a0\u00a0  c\r\n0\u00a0\u00a0 444\u00a0 68\u00a0 46\r\n1\u00a0\u00a0 666\u00a0 62\u00a0 22\r\n2\u00a0\u00a0 888\u00a0 32\u00a0 42\r\n3\u00a0 1110\u00a0 64\u00a0 44\r\n4\u00a0 1332\u00a0 66\u00a0 54\r\n5\u00a0 1554\u00a0 70\u00a0 22<\/pre>\n

    Apply a user-defined function to each row :<\/strong><\/h4>\n

    We just have to add axis=1 to the above function to apply it to rows.<\/p>\n

    #Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n# Multiply given value by 2 and returns\r\ndef doubleData(x):\r\n   return x * 2\r\n#list of tuples\r\nmatrix = [(222, 34, 23),\r\n         (333, 31, 11),\r\n         (444, 16, 21),\r\n         (555, 32, 22),\r\n         (666, 33, 27),\r\n         (777, 35, 11)\r\n         ]\r\n#creating an object from Dataframe class\r\ndfObj = pd.DataFrame(matrix, columns=list('abc'))\r\n#Applyin the user defined function doubleData to rows only\r\nmodMatrix = dfObj.apply(doubleData,axis=1)\r\nprint(\"After applying the user-defined function to each row in dataframe\")\r\nprint(modMatrix)\r\n<\/pre>\n
    Output :\r\nAfter applying the user-defined function to each row in dataframe\r\n \u00a0\u00a0\u00a0\u00a0 a\u00a0\u00a0    b\u00a0\u00a0  c\r\n0\u00a0\u00a0 444\u00a0 68\u00a0 46\r\n1\u00a0\u00a0 666\u00a0 62\u00a0 22\r\n2\u00a0\u00a0 888\u00a0 32\u00a0 42\r\n3\u00a0 1110\u00a0 64\u00a0 44\r\n4\u00a0 1332\u00a0 66\u00a0 54\r\n5\u00a0 1554\u00a0 70\u00a0 22\r\n\r\n\r\n<\/pre>\n

    Apply a user-defined function to each row or column with arguments :<\/strong><\/h4>\n

    Let us take a user defined function that takes accepts a series and a number, then returns the series multiplied to the number<\/p>\n

    #Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#Multplies the whole seried with the number and return the series\r\ndef multiplyData(x, y):\r\n   return x * y\r\n#list of tuples\r\nmatrix = [(222, 34, 23),\r\n         (333, 31, 11),\r\n         (444, 16, 21),\r\n         (555, 32, 22),\r\n         (666, 33, 27),\r\n         (777, 35, 11)\r\n         ]\r\n#creating an object from Dataframe class\r\ndfObj = pd.DataFrame(matrix, columns=list('abc'))\r\n#Applyin the user defined function with a argument\r\nmodMatrix = dfObj.apply(multiplyData, args=[4])\r\nprint(\"After applying the user-defined function with argument in dataframe\")\r\nprint(modMatrix)\r\n<\/pre>\n
    Output :\r\nAfter applying the user-defined function with argument in dataframe\r\n \u00a0\u00a0\u00a0\u00a0 a\u00a0\u00a0\u00a0    b\u00a0\u00a0\u00a0   c\r\n0\u00a0\u00a0 888\u00a0 136\u00a0\u00a0 92\r\n1\u00a0 1332\u00a0 124\u00a0\u00a0 44\r\n2\u00a0 1776\u00a0\u00a0 64\u00a0\u00a0 84\r\n3\u00a0 2220\u00a0 128\u00a0\u00a0 88\r\n4\u00a0 2664\u00a0 132\u00a0 108\r\n5\u00a0 3108\u00a0 140\u00a0\u00a0 44<\/pre>\n

    Apply a numpy functions to each row or column of a Dataframe<\/strong><\/h3>\n

    For this let\u2019s use the numpy function numpy.square( )<\/code>. <\/em>(For columns pass the function directly and for rows add axis=1 and pass)<\/p>\n

    #Program\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#list of tuples\r\nmatrix = [(222, 34, 23),\r\n         (333, 31, 11),\r\n         (444, 16, 21),\r\n         (555, 32, 22),\r\n         (666, 33, 27),\r\n         (777, 35, 11)\r\n         ]\r\n#creating an object from Dataframe class\r\ndfObj = pd.DataFrame(matrix, columns=list('abc'))\r\n#Applyin the numpy fuction .square()\r\nmodMatrix = dfObj.apply(np.square)\r\nprint(\"After applying the numpy function in dataframe\")\r\nprint(modMatrix)\r\n<\/pre>\n
    Output :\r\nAfter applying the numpy function in dataframe\r\n \u00a0\u00a0\u00a0\u00a0\u00a0\u00a0 a\u00a0\u00a0\u00a0\u00a0       b\u00a0\u00a0\u00a0  c\r\n0\u00a0\u00a0 49284\u00a0 1156\u00a0 529\r\n1\u00a0 110889\u00a0\u00a0 961\u00a0 121\r\n2\u00a0 197136\u00a0\u00a0 256\u00a0 441\r\n3\u00a0 308025\u00a0 1024\u00a0 484\r\n4\u00a0 443556\u00a0 1089\u00a0 729\r\n5\u00a0 603729\u00a0 1225\u00a0 121<\/pre>\n

    Apply a Reducing functions to a to each row or column of a Dataframe<\/strong><\/h3>\n

    We passed a series into the user-defined functions and it also returned a series .\u00a0 However, we can also pass a series and return a single variable. Let\u2019s use numpy.sum( )<\/code> for that.<\/p>\n

    #Program :\r\n\r\nimport pandas as pd\r\nimport numpy as np\r\n#list of tuples\r\nmatrix = [(222, 34, 23),\r\n         (333, 31, 11),\r\n         (444, 16, 21),\r\n         (555, 32, 22),\r\n         (666, 33, 27),\r\n         (777, 35, 11)\r\n         ]\r\n#creating an object from Dataframe class\r\ndfObj = pd.DataFrame(matrix, columns=list('abc'))\r\n#Applyin the numpy fuction .sum()\r\nmodMatrix = dfObj.apply(np.sum)\r\nprint(\"After applying the numpy function in datframe\")\r\nprint(modMatrix)\r\n<\/pre>\n
    Output :\r\nAfter applying the numpy function in datframe\r\na\u00a0\u00a0\u00a0 2997\r\nb\u00a0\u00a0\u00a0\u00a0 181\r\nc\u00a0\u00a0\u00a0\u00a0 115\r\ndtype: int64<\/pre>\n

    Want to expert in the python programming language? Exploring\u00a0Python Data Analysis using Pandas<\/a>\u00a0tutorial changes your knowledge from basic to advance level in python concepts.<\/p>\n

    Read more Articles on Python Data Analysis Using Padas \u2013 Modify a Dataframe<\/strong><\/p>\n