嗨,我是Python的新手,我正在尝试使用python学习数据分析,我遇到了这段代码的麻烦

时间:2017-03-21 15:32:30

标签: python

此代码未显示正确的平均值。

from pandas import*
import numpy
m={'ground forces':Series(['30','45','51','66','38'],index=['a','b','c','d','e']),'naval forces':Series(['76','100','91','178','81'],index=['a','b','c','d','e']),'air forces':Series(['212','28','92','77','55'],index=['a','b','c','d','e'])}
k=DataFrame(m)
print(k)
print(k.apply(numpy.mean))

1 个答案:

答案 0 :(得分:0)

似乎您的问题是您在系列的定义中使用字符串而不是数字: 这是正确的代码:

from pandas import*
import numpy

m = {
    'ground forces' : Series([30,45,51,66,38], index=['a', 'b', 'c', 'd', 'e']),
    'naval forces' : Series([76,100,91,178,81], index=['a', 'b', 'c', 'd', 'e']),
    'air forces' : Series([212, 28, 92, 77, 55], index=['a', 'b', 'c', 'd', 'e'])
}

k=DataFrame(m)
print(k)
print(k.apply(numpy.mean))