Square of each element of a column in pandas

Viewed 82390

How can I square each element of a column/series of a DataFrame in pandas (and create another column to hold the result)?

3 Answers

You can also use pandas.DataFrame.pow() method.

>>> import pandas as pd
>>> df = pd.DataFrame([[1,2], [3,4]], columns=list('ab'))
>>> df
   a  b
0  1  2
1  3  4
>>> df['c'] = df['b'].pow(2)
>>> df
   a  b   c
0  1  2   4
1  3  4  16
Related