In this article, we will learn different ways to apply a function to single or selected columns or rows in Dataframe. We will use Dataframe/series.apply() method to apply a function.
Apply a function to single row in Pandas Dataframe
Here, we will use different methods to apply a function to single rows by using Pandas dataframe. First lets create a data frame.
# import pandas and numpy library import pandas as pd
import numpy as np
# List of Tuples matrix = [( 1 , 2 , 3 ),
( 4 , 5 , 6 ),
( 7 , 8 , 9 )]
# Create a DataFrame object df = pd.DataFrame(matrix, columns = list ( 'xyz' ),
index = list ( 'abc' ))
print (df)
|
Output:
x y z
a 1 2 3
b 4 5 6
c 7 8 9
Using Dataframe.apply() and lambda function
Pandas.apply() allow the users to pass a function and apply it on every single value row of the Pandas dataframe. Here, we squared the ‘bth‘ row.
# Apply function numpy.square() to lambda # whose row index is 'b' df = df. apply ( lambda x: np.square(x) if x.name = = 'b' else x,
axis = 1 )
# Output print (df)
|
Output :
x y z
a 1 2 3
b 16 25 36
c 7 8 9
Using loc[ ] Operator
Pandas DataFrame.loc attribute accesses a group of rows in the given DataFrame to squared the ‘bth‘ row.
# import pandas and numpy library import pandas as pd
import numpy as np
# List of Tuples matrix = [( 1 , 2 , 3 ),
( 4 , 5 , 6 ),
( 7 , 8 , 9 )]
# Create a DataFrame object df = pd.DataFrame(matrix, columns = list ( 'xyz' ),
index = list ( 'abc' ))
# Apply a function to one row 'b' # and assign it back to the same row df.loc[ 'b' ] = df.loc[ 'b' ]. apply (np.square)
print (df)
|
Output :
x y z
a 1 2 3
b 16 25 36
c 7 8 9
Using numpy.square() method
This mathematical function helps the user to calculate the square value of each element in the array. Here, we are passing b row to make a square of it.
# import pandas and numpy library import pandas as pd
import numpy as np
# List of Tuples matrix = [( 1 , 2 , 3 ),
( 4 , 5 , 6 ),
( 7 , 8 , 9 )]
# Create a DataFrame object df = pd.DataFrame(matrix, columns = list ( 'xyz' ),
index = list ( 'abc' ))
# Apply a function to one row 'b' and # assign it back to the same row df.loc[ 'b' ] = np.square(df.loc[ 'b' ])
# Output print (df)
|
Output :
x y z
a 1 2 3
b 16 25 36
c 7 8 9
Apply a function to single column in Pandas Dataframe
Here, we will use different methods to apply a function to single columns by using Pandas Dataframe.
Using Dataframe.apply() and lambda function
Pandas.apply() allow the users to pass a function and apply it on every single value column of the Pandas Dataframe. Here, we squared the ‘zth‘ column.
# import pandas and numpy library import pandas as pd
import numpy as np
# List of Tuples matrix = [( 1 , 2 , 3 ),
( 4 , 5 , 6 ),
( 7 , 8 , 9 )]
# Create a DataFrame object df = pd.DataFrame(matrix, columns = list ( 'xyz' ),
index = list ( 'abc' ))
# Apply function numpy.square() to lambda # to find the squares of the values of # column whose column name is 'z' new_df = df. apply ( lambda x: np.square(x) if x.name = = 'z' else x)
# Output print (new_df)
|
Output :
x y z
a 1 2 9
b 4 5 36
c 7 8 81
Using loc[ ] Operator
Pandas DataFrame.loc attribute access a group of columns in the given DataFrame to square the ‘zth‘ column.
# import pandas and numpy library import pandas as pd
import numpy as np
# List of Tuples matrix = [( 1 , 2 , 3 ),
( 4 , 5 , 6 ),
( 7 , 8 , 9 )]
# Create a DataFrame object df = pd.DataFrame(matrix, columns = list ( 'xyz' ),
index = list ( 'abc' ))
# Apply a function to one column 'z' # and assign it back to the same column df[ 'z' ] = df[ 'z' ]. apply (np.square)
# Output print (df)
|
Output :
x y z
a 1 2 9
b 4 5 36
c 7 8 81
Using numpy.square() method
This mathematical function helps the user to calculate the square value of each element in the array. Here, we are passing z column to make a square of it.
# import pandas and numpy library import pandas as pd
import numpy as np
# List of Tuples matrix = [( 1 , 2 , 3 ),
( 4 , 5 , 6 ),
( 7 , 8 , 9 )]
# Create a DataFrame object df = pd.DataFrame(matrix, columns = list ( 'xyz' ),
index = list ( 'abc' ))
# Apply a function to one column 'z' and # assign it back to the same column df[ 'z' ] = np.square(df[ 'z' ])
# Output print (df)
|
Output:
x y z
a 1 2 9
b 4 5 36
c 7 8 81
Apply function to column and row in the Dataframe
Here. we will see how to apply a function to more than one row and column using df.apply() method.
For Column
Here, we applied the function to the x, and y columns.
# import pandas and numpy library import pandas as pd
import numpy as np
# List of Tuples matrix = [( 1 , 2 , 3 ),
( 4 , 5 , 6 ),
( 7 , 8 , 9 )]
# Create a DataFrame object df = pd.DataFrame(matrix, columns = list ( 'xyz' ),
index = list ( 'abc' ))
# Apply function numpy.square() # for square the values of # two columns 'x' and 'y' new_df = df. apply ( lambda x: np.square(x) if x.name in [ 'x' , 'y' ] else x)
# Output print (new_df)
|
Output:
x y z
a 1 4 3
b 16 25 6
c 49 64 9
For Row
Here, we applied the function to the b, and c rows.
# import pandas and numpy library import pandas as pd
import numpy as np
# List of Tuples matrix = [( 1 , 2 , 3 ),
( 4 , 5 , 6 ),
( 7 , 8 , 9 )]
# Create a DataFrame object df = pd.DataFrame(matrix, columns = list ( 'xyz' ),
index = list ( 'abc' ))
# Apply function numpy.square() to # square the values of two rows # 'b' and 'c' new_df = df. apply ( lambda x: np.square(x) if x.name in [ 'b' , 'c' ] else x,
axis = 1 )
# Output print (new_df)
|
Output:
x y z
a 1 2 3
b 16 25 36
c 49 64 81