{"id":3117,"date":"2021-04-22T07:59:48","date_gmt":"2021-04-22T02:29:48","guid":{"rendered":"https:\/\/python-programs.com\/?p=3117"},"modified":"2021-11-22T18:54:11","modified_gmt":"2021-11-22T13:24:11","slug":"pandas-apply-a-function-to-single-or-selected-columns-or-rows-in-dataframe","status":"publish","type":"post","link":"https:\/\/python-programs.com\/pandas-apply-a-function-to-single-or-selected-columns-or-rows-in-dataframe\/","title":{"rendered":"Pandas: Apply a function to single or selected columns or rows in Dataframe"},"content":{"rendered":"
In this article, we will be applying given function to selected rows and column.<\/p>\n
For example, we have a dataframe object,<\/p>\n
matrix = <\/span>[(<\/span>22<\/span>, <\/span>34<\/span>, <\/span>23<\/span>)<\/span>,\r\n<\/span>(33<\/span>, <\/span>31<\/span>, <\/span>11<\/span>)<\/span>,\r\n<\/span>(44<\/span>, <\/span>16<\/span>, <\/span>21<\/span>)<\/span>,\r\n<\/span>(55<\/span>, <\/span>32<\/span>, <\/span>22<\/span>)<\/span>,\r\n<\/span>(66<\/span>, <\/span>33<\/span>, <\/span>27<\/span>)<\/span>,\r\n<\/span>(77<\/span>, <\/span>35<\/span>, <\/span>11<\/span>)\r\n<\/span>]\r\n# Create a DataFrame object\r\ndfObj = pd.DataFrame<\/span>(<\/span>matrix, columns=<\/span>list<\/span>(<\/span>'xyz'<\/span>)<\/span>, index=<\/span>list<\/span>(<\/span>'abcdef'<\/span>))<\/span><\/pre>\n<\/div>\nContents of this dataframe object dgObj are,<\/div>\n\n\nOriginal Dataframe\r\n<\/span> x y z\r\na 22<\/span> 34<\/span> 23\r\n<\/span>b 33<\/span> 31<\/span> 11\r\n<\/span>c 44<\/span> 16<\/span> 21\r\n<\/span>d 55<\/span> 32<\/span> 22\r\n<\/span>e 66<\/span> 33<\/span> 27\r\n<\/span>f 77<\/span> 35<\/span> 11<\/span><\/pre>\nNow what if we want to apply different functions on all the elements of a single or multiple column or rows. Like,<\/p>\n
\n- Multiply all the values in column \u2018x\u2019 by 2<\/li>\n
- Multiply all the values in row \u2018c\u2019 by 10<\/li>\n
- Add 10 in all the values in column \u2018y\u2019 & \u2018z\u2019<\/li>\n<\/ul>\n
We will use different techniques to see how we can do this.<\/p>\n
Apply a function to a single column in Dataframe<\/h2>\n
What if we want to square all the values in any of the column for example x,y or z.<\/p>\n
We can do such things by applying different methods. We will discuss few methods below:<\/p>\n
Method 1: Using Dataframe.apply()<\/strong><\/h4>\nWe will apply lambda function to all the columns using the above method. And then we will check if column name is whatever we want say x,y or z inside the lambda function. After this, we will square all the values. In this we will be taking z column.<\/p>\n
<\/p>\n
Code:<\/p>\n
\nmodDfObj = dfObj.<\/span>apply<\/span>(<\/span>lambda<\/span> x: np.<\/span>square<\/span>(<\/span>x<\/span>)<\/span> if<\/span> x.name == <\/span>'z'<\/span> else<\/span> x<\/span>)\r\n<\/span>print(<\/span>\"Modified Dataframe : Squared the values in column 'z'\"<\/span>, modDfObj, sep=<\/span>'\\n'<\/span>)<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\nOutput:<\/div>\n\n\nModified Dataframe : Squared the values <\/span>in<\/span> column <\/span>'z'\r\n<\/span> x y z\r\na 22<\/span> 34<\/span> 529\r\n<\/span>b 33<\/span> 31<\/span> 121\r\n<\/span>c 44<\/span> 16<\/span> 441\r\n<\/span>d 55<\/span> 32<\/span> 484\r\n<\/span>e 66<\/span> 33<\/span> 729\r\n<\/span>f 77<\/span> 35<\/span> 121<\/span><\/pre>\n<\/div>\n<\/div>\nMethod 2: Using [] operator<\/h4>\n
Using [] operator we will select the column from dataframe and apply numpy.square() method. Later, we will assign it back to the column.<\/p>\n
dfObj<\/span>[<\/span>'z'<\/span>]<\/span> = dfObj<\/span>[<\/span>'z'<\/span>]<\/span>.<\/span>apply<\/span>(<\/span>np.square<\/span>)<\/span><\/pre>\nIt will square all the values in column ‘z’.<\/p>\n
Method 3: Using numpy.square()<\/h4>\ndfObj<\/span>[<\/span>'z'<\/span>]<\/span> = np.<\/span>square<\/span>(<\/span>dfObj<\/span>[<\/span>'z'<\/span>])<\/span><\/pre>\nThis function will also square all the values in ‘z’.<\/p>\n
Apply a function to a single row in Dataframe<\/h2>\n
Now, we saw what we have done with the columns. Same thing goes with rows. We will square all the values in row ‘b’. We can use different methods for that.<\/p>\n
Method 1:Using\u00a0Dataframe.apply()<\/a><\/strong><\/h4>\nWe will apply lambda function to all the rows and will use the above function. We will check the label inside the lambda function and will square the row.<\/p>\n
<\/p>\n
Code:<\/p>\n
\n\n\nmodDfObj = dfObj.<\/span>apply<\/span>(<\/span>lambda<\/span> x: np.<\/span>square<\/span>(<\/span>x<\/span>)<\/span> if<\/span> x.name == <\/span>'b'<\/span> else<\/span> x, axis=<\/span>1<\/span>)\r\n<\/span>print(<\/span>\"Modified Dataframe : Squared the values in row 'b'\"<\/span>, modDfObj, sep=<\/span>'\\n'<\/span>)<\/span><\/pre>\n<\/div>\n<\/div>\n<\/div>\nOutput:<\/p>\n
\nModified Dataframe : Squared the values <\/span>in<\/span> row <\/span>'b'\r\n<\/span> x y z\r\na 22<\/span> 34<\/span> 23\r\n<\/span>b 1089<\/span> 961<\/span> 121\r\n<\/span>c 44<\/span> 16<\/span> 21\r\n<\/span>d 55<\/span> 32<\/span> 22\r\n<\/span>e 66<\/span> 33<\/span> 27\r\n<\/span>f 77<\/span> 35<\/span> 11<\/span><\/pre>\n<\/div>\nMethod 2 : Using [] Operator<\/strong><\/h4>\nWe will do what we have done above. We will select the row from dataframe.loc[] operator and apply numpy.square() method on it. Later, we will assign it back to the row.<\/p>\n
dfObj.loc<\/span>[<\/span>'b'<\/span>]<\/span> = dfObj.loc<\/span>[<\/span>'b'<\/span>]<\/span>.<\/span>apply<\/span>(<\/span>np.square<\/span>)<\/span><\/pre>\nIt will square all the values in the row ‘b’.<\/p>\n
Method 3 : Using numpy.square()<\/h4>\ndfObj.loc<\/span>[<\/span>'b'<\/span>]<\/span> = np.<\/span>square<\/span>(<\/span>dfObj.loc<\/span>[<\/span>'b'<\/span>])<\/span><\/pre>\nThis will also square the values in row ‘b’.<\/p>\n
Apply a function to a certain columns in Dataframe<\/h2>\n
We can apply the function in whichever column we want. For instance, squaring the values in ‘x’ and ‘y’.<\/p>\n
<\/p>\n
Code:<\/p>\n
\nmodDfObj = dfObj.<\/span>apply<\/span>(<\/span>lambda<\/span> x: np.<\/span>square<\/span>(<\/span>x<\/span>)<\/span> if<\/span> x.name <\/span>in<\/span> [<\/span>