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.
Python3
import pandas as pd
import numpy as np
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
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
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 ‘b th ‘ row.
Python3
df = df.apply(lambda x: np.square(x) if x.name == 'b' else x,
axis = 1)
print(df)
Output :
x y z a 1 2 3 b 16 25 36 c 7 8 9
Pandas DataFrame.loc attribute accesses a group of rows in the given DataFrame to squared the ‘b th ‘ row.
Python3
import pandas as pd
import numpy as np
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
df = pd.DataFrame(matrix, columns=list('xyz'),
index=list('abc'))
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
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 as pd
import numpy as np
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
df = pd.DataFrame(matrix, columns=list('xyz'),
index=list('abc'))
df.loc['b'] = np.square(df.loc['b'])
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.
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 ‘z th ‘ column.
Python3
import pandas as pd
import numpy as np
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
df = pd.DataFrame(matrix, columns=list('xyz'),
index=list('abc'))
new_df = df.apply(lambda x: np.square(x) if x.name == 'z' else x)
print(new_df)
Output :
x y z a 1 2 9 b 4 5 36 c 7 8 81
Pandas DataFrame.loc attribute access a group of columns in the given DataFrame to square the ‘z th ‘ column.
Python3
import pandas as pd
import numpy as np
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
df = pd.DataFrame(matrix, columns=list('xyz'),
index=list('abc'))
df['z'] = df['z'].apply(np.square)
print(df)
Output :
x y z a 1 2 9 b 4 5 36 c 7 8 81
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 as pd
import numpy as np
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
df = pd.DataFrame(matrix, columns=list('xyz'),
index=list('abc'))
df['z'] = np.square(df['z'])
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.
Python3
import pandas as pd
import numpy as np
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
df = pd.DataFrame(matrix, columns=list('xyz'),
index=list('abc'))
new_df = df.apply(lambda x: np.square(x) if x.name in ['x', 'y'] else x)
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.
Python3
import pandas as pd
import numpy as np
matrix = [(1, 2, 3),
(4, 5, 6),
(7, 8, 9)]
df = pd.DataFrame(matrix, columns=list('xyz'),
index=list('abc'))
new_df = df.apply(lambda x: np.square(x) if x.name in ['b', 'c'] else x,
axis=1)
print(new_df)
Output:
x y z a 1 2 3 b 16 25 36 c 49 64 81
Don't miss your chance to ride the wave of the data revolution! Every industry is scaling new heights by tapping into the power of data. Sharpen your skills and become a part of the hottest trend in the 21st century.
Dive into the future of technology - explore the Complete Machine Learning and Data Science Program by GeeksforGeeks and stay ahead of the curve.
Last Updated :
04 Sep, 2023
Like Article
Save Article
Share your thoughts in the comments
Add Your Comment
Please Login to comment...