Getting the square of the column in pandas python can be done in two ways using power function. Let’s see how to
- Get the square of the column in pandas python
With examples
First let’s create a dataframe
import pandas as pd import numpy as np df1 = { 'State':['Arizona AZ','Georgia GG','Newyork NY','Indiana IN','Florida FL'], 'Score':[4,47,55,74,31]} df1 = pd.DataFrame(df1,columns=['State','Score']) print(df1)
df1 will be
Square of the column in pandas– Method 1:
Simply square the column and store in other column as shown below
df1['Score_Square']=df1['Score']**2 print(df1)
So the resultant dataframe will be
Square of the column in pandas – Method 2:
Square the column using power function and store it in other column as shown below
df1['Score_square']=np.power((df1['Score']),2) print(df1)
So the resultant dataframe will be