Skip to content
Related Articles
Open in App
Not now

Related Articles

Apply a function to single or selected columns or rows in Pandas Dataframe

Improve Article
Save Article
  • Last Updated : 22 Mar, 2023
Improve Article
Save Article

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 rows in Pandas Dataframe

Here, we will use different methods to apply a function to single rows 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 row of the Pandas Dataframe. Here, we squared the ‘bth‘ row.

Python3




# 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
# whose row index is 'b'
new_df = df.apply(lambda x: np.square(x) if x.name == 'b' else x,
                axis = 1)
 
# Output
new_df

Output :

 

Using loc[ ] Operator

Pandas DataFrame.loc attribute accesses a group of rows in the given DataFrame to squared the ‘bth‘ row.

Python3




# 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)
 
df

Output :

 

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.

Python3




# 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
df

Output :

 

Apply a function to single columns 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.

Python3




# 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
new_df

Output :

 

Using loc[ ] Operator 

Pandas DataFrame.loc attribute access a group of columns in the given DataFrame to square the ‘zth‘ column.

Python3




# 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
df

Output :

 

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.

Python3




# 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:

 

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.

Python3




# 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
new_df

Output:

 

For Row

Here, we applied the function to the b, and c rows.

Python3




# 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
new_df

Output:

 


My Personal Notes arrow_drop_up
Related Articles

Start Your Coding Journey Now!