问题描述
我正在尝试Julia DataFrames模块。我对它感兴趣,所以我可以用它来绘制Gadfly中的简单模拟。我想要能够迭代地向数据框中添加行,我想将其初始化为空。有关如何执行此操作的教程/文档是稀疏的(大多数文档描述了如何分析导入的数据)。
附加到非空数据框是很简单的:
df = DataFrame(A = [1,2],B = [4,5])
push!(df,[3 6])
返回。
3x2 DataFrame
|行| A | B |
| ----- | --- | --- |
| 1 | 1 | 4 |
| 2 | 2 | 5 |
| 3 | 3 | 6 |
但是对于一个空的init我收到错误。
df = DataFrame(A = [],B = [])
push!(df,[3, 6])
错误信息:
ArgumentError(错误添加3到列:A。可能的类型不匹配。)
加载在[220]中,从第2行开始的表达式
什么是初始化空的Julia DataFrame的最佳方式,以便您可以在for循环中迭代地将项目添加到其中?
仅使用 [] 定义的零长度数组将缺少足够的类型信息。 >
julia> typeof([])
数组{None,1}
所以为避免这个问题简单地指出类型。
julia> typeof(Int64 [])
数组{Int64,1}
你可以应用您的DataFrame问题
julia> df = DataFrame(A = Int64 [],B = Int64 [])
0x2 DataFrame
julia> push!(df,[3 6])
julia> df
1x2 DataFrame
|行| A | B |
| ----- | --- | --- |
| 1 | 3 | 6 |
I am trying out the Julia DataFrames module. I am interested in it so I can use it to plot simple simulations in Gadfly. I want to be able to iteratively add rows to the dataframe and I want to initialize it as empty.
The tutorials/documentation on how to do this is sparse (most documentation describes how to analyse imported data).
To append to a nonempty dataframe is straightforward:
df = DataFrame(A = [1, 2], B = [4, 5])
push!(df, [3 6])
This returns.
3x2 DataFrame
| Row | A | B |
|-----|---|---|
| 1 | 1 | 4 |
| 2 | 2 | 5 |
| 3 | 3 | 6 |
But for an empty init I get errors.
df = DataFrame(A = [], B = [])
push!(df, [3, 6])
Error message:
ArgumentError("Error adding 3 to column :A. Possible type mis-match.")
while loading In[220], in expression starting on line 2
What is the best way to initialize an empty Julia DataFrame such that you can iteratively add items to it later in a for loop?
A zero length array defined using only [] will lack sufficient type information.
julia> typeof([])
Array{None,1}
So to avoid that problem is to simply indicate the type.
julia> typeof(Int64[])
Array{Int64,1}
And you can apply that to your DataFrame problem
julia> df = DataFrame(A = Int64[], B = Int64[])
0x2 DataFrame
julia> push!(df, [3 6])
julia> df
1x2 DataFrame
| Row | A | B |
|-----|---|---|
| 1 | 3 | 6 |
这篇关于julia创建一个空的数据框并附加行的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!