Reputation: 663
I have data frame like
in the CSV.
I have received error:
91 return converter(self.iloc[0])
92 raise TypeError("cannot convert the series to "
---> 93 "{0}".format(str(converter)))
94
95 wrapper.__name__ = "__{name}__".format(name=converter.__name__)
TypeError: cannot convert the series to
Following is my code
import pandas as pd
data = pd.read_csv("test.csv")
a=data['a']
b=data['b']
value=sqrt(a**2+b**2)
data['value']=value
Upvotes: 1
Views: 36
Reputation: 432
Since the sqrt
is not an inbuilt function like sum()
in python, you need to use packages like Numpy
or Math
to call the sqrt
function.
import numpy as np
np.sqrt(a**2+b**2)
or
import math
math.sqrt(a**2+b**2)
Upvotes: 0
Reputation: 486
One way is to use list comprehension:
value=[math.sqrt(x**2+y**2) for x,y in zip(a,b)]
Full code example:
import pandas as pd
import math
data = pd.read_csv("test.csv")
a=data['a']
b=data['b']
value=[math.sqrt(x**2+y**2) for x,y in zip(a,b)]
data['value']=value
Or use numpy
here to do the element wise operation.
import numpy as np
...
np.sqrt(..)
So this code works as expected
import pandas as pd
import numpy as np
data = pd.read_csv("test.csv")
a=data['a']
b=data['b']
value=np.sqrt(a**2+b**2)
data['value']=value
Adds a column with designated value which is element wise square sum of first 2 columns.
Upvotes: 1