【发布时间】:2019-11-23 12:35:53
【问题描述】:
我有以下数据框
using DataFrames, Statistics
df = DataFrame(name=["John", "Sally", "Kirk"],
age=[23., 42., 59.],
children=[3,5,2], height = [180, 150, 170])
print(df)
3×4 DataFrame
│ Row │ name │ age │ children │ height │
│ │ String │ Float64 │ Int64 │ Int64 │
├─────┼────────┼─────────┼──────────┼────────┤
│ 1 │ John │ 23.0 │ 3 │ 180 │
│ 2 │ Sally │ 42.0 │ 5 │ 150 │
│ 3 │ Kirk │ 59.0 │ 2 │ 170 │
我可以按如下方式计算列的平均值:
println(mean(df[:4]))
166.66666666666666
现在我想获取所有数字列的平均值并尝试了以下代码:
x = [2,3,4]
for i in x
print(mean(df[:x[i]]))
end
但收到以下错误消息:
MethodError: no method matching getindex(::Symbol, ::Int64)
Stacktrace:
[1] top-level scope at ./In[64]:3
我该如何解决这个问题?
【问题讨论】:
-
作为一般评论 - 很快
df[col]语法将被弃用。所以几天后推荐的模式将是df[!, col]访问列而不复制它,df[:, col]复制它。