【问题标题】:describe() returns "ERROR: UndefVarError: describe not defined"describe() 返回“错误:UndefVarError:描述未定义”
【发布时间】:2019-04-26 05:55:34
【问题描述】:

我将一个表从 SQL 数据库导入到数据框中,现在我正尝试通过 describe() 获取有关数据框的统计信息。我也试过head()。两者都返回错误"ERROR: UndefVarError: describe not defined"

我已经添加并导入了DataFrames 包来解决这个问题,但是没有用。

这就是我导入数据框的方式:

using Pkg

Pkg.add("ODBC")

Pkg.add("DataFrames")

using ODBC, DataFrames

db = ODBC.DSN(connection_string)

query = ODBC.query(db, "SELECT * FROM table")

df = DataFrame(query)

describe(df)

我期望得到类似于describe()head() Python 函数的结果。在运行head(df) 后,我希望列标签和前几行。在运行describe(df) 之后,我希望每个列标签的最小值、最大值、平均值、计数等。

【问题讨论】:

  • 您是否还导入了Pandas.jl 或任何其他提供describehead 的包?在这种情况下,您需要使用完全限定名称DataFrames.describeDataFrames.head。这在其他情况下是不必要的。
  • 谢谢!我很惊讶我正在阅读的 Julia 页面没有在任何地方提到这一点。

标签: dataframe julia


【解决方案1】:

first 而不是head。请参阅下面的代码以获取示例:

julia> using DataFrames

julia> df = DataFrame(a=1:5,b=6:10)
5×2 DataFrame
│ Row │ a     │ b     │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 6     │
│ 2   │ 2     │ 7     │
│ 3   │ 3     │ 8     │
│ 4   │ 4     │ 9     │
│ 5   │ 5     │ 10    │

julia> first(df,3)
3×2 DataFrame
│ Row │ a     │ b     │
│     │ Int64 │ Int64 │
├─────┼───────┼───────┤
│ 1   │ 1     │ 6     │
│ 2   │ 2     │ 7     │
│ 3   │ 3     │ 8     │

julia> describe(df)
2×8 DataFrame
│ Row │ variable │ mean    │ min   │ median  │ max   │ nunique │ nmissing │ eltype   │
│     │ Symbol   │ Float64 │ Int64 │ Float64 │ Int64 │ Nothing │ Nothing  │ DataType │
├─────┼──────────┼─────────┼───────┼─────────┼───────┼─────────┼──────────┼──────────┤
│ 1   │ a        │ 3.0     │ 1     │ 3.0     │ 5     │         │          │ Int64    │
│ 2   │ b        │ 8.0     │ 6     │ 8.0     │ 10    │         │          │ Int64    │

【讨论】:

  • head 目前在那里,尽管已被弃用。我不认为 OP 得到的错误是关于 head 被弃用。
  • @hckr 请注意,通过由另一个人覆盖方法定义,他不应该得到UndefVarError。无论如何,您提到的完全限定名称对于名称冲突场景确实很有用:-)
  • 我在评论中的意思是像using Plots, GR; plot(rand(5)) 这样的情况。在这种情况下,您将收到一个UndefVarError,并在您第一次尝试访问plot 时伴随警告。
  • 我喜欢你的例子。虽然我刚刚检查过,只有在单个 using 语句中导入 2 个包时才会发生这种情况。
  • 不一定,试试using Plots; using GR; plot(rand(5))。我相信除非你在using GR 之前在你的代码中调用plot,否则你仍然会得到UndefVarError。所以using Plots; ...write as many statements that do not call plot; using GR; plot 仍然会给你一个UndefVarError。但是,如果您在此之前确实调用了plot,那么没有限定符的plot 将指向Plots.plot,如果您需要访问GR.plot,您仍然需要限定plot
猜你喜欢
  • 2017-12-09
  • 1970-01-01
  • 1970-01-01
  • 2014-07-02
  • 1970-01-01
  • 2018-12-08
  • 2018-02-21
  • 1970-01-01
  • 1970-01-01
相关资源
最近更新 更多