简体   繁体   English

不知道如何强制 int64 和 float64

[英]don't know how to coerce int64 and float64

I have a list which is filled form excel file (import to pandas)我有一个由 excel 文件填写的列表(导入到 Pandas)

a=[df.math[0],df.bio[0],df.chemistry[0]]

when I pass mean(a) it gives me the following error:当我通过 mean(a) 时,它给了我以下错误:

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我试过a=[float(df.math[0]),float(df.bio[0]),float(df.chemistry[0])] - 但仍然不起作用

What do you think is the problem ?你认为有什么问题?

I think here the simpliest is use numpy.mean :我认为这里最简单的是使用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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM