Skip to content
Advertisement

How do I square a column from an Excel file with pandas?

I’ve read an Excel file into python using:

import pandas as pd
import numpy as np

water_consumption = pd.read_csv('Self_Data.csv')

and I’m trying to square the columns using:

exponent = 2
water_consumption['x2'] = np.power(water_consumption['Consumption_(HCF)'], exponent)
water_consumption['y2'] = np.power(water_consumption['Water&Sewer_Charges'], exponent)

I keep getting the error:

TypeError: unsupported operand type(s) for ** or pow(): 'str' and 'int'

I’m fairly new to python. Is there any way to easily fix this?

Advertisement

Answer

Never use apply-lambda for straightforward mathematical operations it is orders of magnitude slower than using direct operations. The problem that click004 is having is that columns are in str format.

They should be converted first to a numeric, typically with .convert_dtypes(): https://pandas.pydata.org/docs/reference/api/pandas.DataFrame.convert_dtypes.html

Pandas is quite good at understanding the type of column, the fact that it has been detected as str means that probably some of the values are not directly numbers, but may have units or something else.

User contributions licensed under: CC BY-SA
7 People found this is helpful
Advertisement