此代码未显示正确的平均值。
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))
答案 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))