In this article, we will be applying given function to selected rows and column.
For example, we have a dataframe object,
matrix = [(22, 34, 23), (33, 31, 11), (44, 16, 21), (55, 32, 22), (66, 33, 27), (77, 35, 11) ] # Create a DataFrame object dfObj = pd.DataFrame(matrix, columns=list('xyz'), index=list('abcdef'))
Original Dataframe x y z a 22 34 23 b 33 31 11 c 44 16 21 d 55 32 22 e 66 33 27 f 77 35 11
Now what if we want to apply different functions on all the elements of a single or multiple column or rows. Like,
- Multiply all the values in column ‘x’ by 2
- Multiply all the values in row ‘c’ by 10
- Add 10 in all the values in column ‘y’ & ‘z’
We will use different techniques to see how we can do this.
Apply a function to a single column in Dataframe
What if we want to square all the values in any of the column for example x,y or z.
We can do such things by applying different methods. We will discuss few methods below:
Method 1: Using Dataframe.apply()
We 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.
Code:
modDfObj = dfObj.apply(lambda x: np.square(x) if x.name == 'z' else x) print("Modified Dataframe : Squared the values in column 'z'", modDfObj, sep='\n')
Modified Dataframe : Squared the values in column 'z' x y z a 22 34 529 b 33 31 121 c 44 16 441 d 55 32 484 e 66 33 729 f 77 35 121
Method 2: Using [] operator
Using [] operator we will select the column from dataframe and apply numpy.square() method. Later, we will assign it back to the column.
dfObj['z'] = dfObj['z'].apply(np.square)
It will square all the values in column ‘z’.
Method 3: Using numpy.square()
dfObj['z'] = np.square(dfObj['z'])
This function will also square all the values in ‘z’.
Apply a function to a single row in Dataframe
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.
Method 1:Using Dataframe.apply()
We 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.
Code:
modDfObj = dfObj.apply(lambda x: np.square(x) if x.name == 'b' else x, axis=1) print("Modified Dataframe : Squared the values in row 'b'", modDfObj, sep='\n')
Output:
Modified Dataframe : Squared the values in row 'b' x y z a 22 34 23 b 1089 961 121 c 44 16 21 d 55 32 22 e 66 33 27 f 77 35 11
Method 2 : Using [] Operator
We 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.
dfObj.loc['b'] = dfObj.loc['b'].apply(np.square)
It will square all the values in the row ‘b’.
Method 3 : Using numpy.square()
dfObj.loc['b'] = np.square(dfObj.loc['b'])
This will also square the values in row ‘b’.
Apply a function to a certain columns in Dataframe
We can apply the function in whichever column we want. For instance, squaring the values in ‘x’ and ‘y’.
Code:
modDfObj = dfObj.apply(lambda x: np.square(x) if x.name in ['x', 'y'] else x) print("Modified Dataframe : Squared the values in column x & y :", modDfObj, sep='\n')
Apply a function to a certain rows in Dataframe
We can apply the function to specified row. For instance, row ‘b’ and ‘c’.
Code:
modDfObj = dfObj.apply(lambda x: np.square(x) if x.name in ['b', 'c'] else x, axis=1) print("Modified Dataframe : Squared the values in row b & c :", modDfObj, sep='\n')
The complete code is:
import pandas as pd import numpy as np def main(): # List of Tuples matrix = [(22, 34, 23), (33, 31, 11), (44, 16, 21), (55, 32, 22), (66, 33, 27), (77, 35, 11)] # Create a DataFrame object dfObj = pd.DataFrame(matrix, columns=list('xyz'), index=list('abcdef')) print("Original Dataframe", dfObj, sep='\n') print('********* Apply a function to a single row or column in DataFrame ********') print('*** Apply a function to a single column *** ') # Method 1: # Apply function numpy.square() to square the value one column only i.e. with column name 'z' modDfObj = dfObj.apply(lambda x: np.square(x) if x.name == 'z' else x) print("Modified Dataframe : Squared the values in column 'z'", modDfObj, sep='\n') # Method 2 # Apply a function to one column and assign it back to the column in dataframe dfObj['z'] = dfObj['z'].apply(np.square) # Method 3: # Apply a function to one column and assign it back to the column in dataframe dfObj['z'] = np.square(dfObj['z'] print('*** Apply a function to a single row *** ') dfObj = pd.DataFrame(matrix, columns=list('xyz'), index=list('abcdef')) # Method 1: # Apply function numpy.square() to square the values of one row only i.e. row with index name 'b' modDfObj = dfObj.apply(lambda x: np.square(x) if x.name == 'b' else x, axis=1) print("Modified Dataframe : Squared the values in row 'b'", modDfObj, sep='\n') # Method 2: # Apply a function to one row and assign it back to the row in dataframe dfObj.loc['b'] = dfObj.loc['b'].apply(np.square) # Method 3: # Apply a function to one row and assign it back to the column in dataframe dfObj.loc['b'] = np.square(dfObj.loc['b']) print('********* Apply a function to certains row or column in DataFrame ********') dfObj = pd.DataFrame(matrix, columns=list('xyz'), index=list('abcdef')) print('Apply a function to certain columns only') # Apply function numpy.square() to square the value 2 column only i.e. with column names 'x' and 'y' only modDfObj = dfObj.apply(lambda x: np.square(x) if x.name in ['x', 'y'] else x) print("Modified Dataframe : Squared the values in column x & y :", modDfObj, sep='\n') print('Apply a function to certain rows only') # Apply function numpy.square() to square the values of 2 rows only i.e. with row index name 'b' and 'c' only modDfObj = dfObj.apply(lambda x: np.square(x) if x.name in ['b', 'c'] else x, axis=1) print("Modified Dataframe : Squared the values in row b & c :", modDfObj, sep='\n') if __name__ == '__main__': main()
Original Dataframe x y z a 22 34 23 b 33 31 11 c 44 16 21 d 55 32 22 e 66 33 27 f 77 35 11 ********* Apply a function to a single row or column in DataFrame ******** *** Apply a function to a single column *** Modified Dataframe : Squared the values in column 'z' x y z a 22 34 529 b 33 31 121 c 44 16 441 d 55 32 484 e 66 33 729 f 77 35 121 *** Apply a function to a single row *** Modified Dataframe : Squared the values in row 'b' x y z a 22 34 23 b 1089 961 121 c 44 16 21 d 55 32 22 e 66 33 27 f 77 35 11 ********* Apply a function to certains row or column in DataFrame ******** Apply a function to certain columns only Modified Dataframe : Squared the values in column x & y : x y z a 484 1156 23 b 1089 961 11 c 1936 256 21 d 3025 1024 22 e 4356 1089 27 f 5929 1225 11 Apply a function to certain rows only Modified Dataframe : Squared the values in row b & c : x y z a 22 34 23 b 1089 961 121 c 1936 256 441 d 55 32 22 e 66 33 27 f 77 35 11
I hope you understood this article well.
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.apply(): Apply a function to each row/column in Dataframe
- Pandas: Sort rows or columns in Dataframe based on values using Dataframe.sort_values()
- Sort a DataFrame based on column names or row index labels using Dataframe.sort_index() in Pandas
- Change data type of single or multiple columns of Dataframe in Python
- Change Column & Row names in DataFrame
- Convert Dataframe column type from string to date time
- Convert Dataframe column into to the Index of Dataframe
- Convert Dataframe indexes into columns