Back

Explore Courses Blog Tutorials Interview Questions
0 votes
2 views
in Data Science by (50.2k points)

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

1 Answer

0 votes
by (108k points)

The following piece of code works for me: 

>>> 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']**2

>>> df

   a  b   c

0  1  2   4

1  3  4  16

If you wish to Learn more about Pandas visit this Pandas Tutorial.

Browse Categories

...