I have a list which is filled form excel file (import to pandas)
a=[df.math[0],df.bio[0],df.chemistry[0]]
when I pass mean(a) it gives me the following error:
don't know how to coerce int64 and float64
How to fix it? I tried a=[float(df.math[0]),float(df.bio[0]),float(df.chemistry[0])]
- but still does not work
What do you think is the problem ?
I think here the simpliest is use numpy.mean
:
x = np.mean(a)
Sample :
df = pd.DataFrame({
'A':list('abcdef'),
'math':[4,5,4,5,5,4],
'chemistry':[7.3434,8,9,4,2,3],
'bio':[1,3,5,7,1,0],
'E':[5,3,6,9,2,4],
'F':list('aaabbb')
})
x = np.mean(a)
print (x)
4.114466666666666
Pandas solution:
x = df.loc[0, ['math','bio','chemistry']].mean()
print (x)
4.114466666666666
Also for me working converting all values to floats:
import statistics
a = [float(df.math[0]),float(df.bio[0]),float(df.chemistry[0])]
x = statistics.mean(a)
print (x)
4.114466666666667
import statistics
a = [float(x) for x in a]
x = statistics.mean(a)
print (x)
4.114466666666667
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.