gpt4 book ai didi

dataframe - 如何将向量添加到 Julia 中某个数组的列中?

转载 作者:行者123 更新时间:2023-12-04 06:39:34 28 4
gpt4 key购买 nike

我知道,包 DataFrames ,可以通过简单地做

julia> df = DataFrame();

julia> for i in 1:3
df[i] = [i, i+1, i*2]
end

julia> df
3x3 DataFrame
|-------|----|----|----|
| Row # | x1 | x2 | x3 |
| 1 | 1 | 2 | 3 |
| 2 | 2 | 3 | 4 |
| 3 | 2 | 4 | 6 |

...但是有没有办法在空的 Array{Int64,2} 上做同样的事情? ?

最佳答案

如果您知道最终数组中有多少行,可以使用 hcat 来完成。 :

# The number of lines of your final array
numrows = 3

# Create an empty array of the same type that you want, with 3 rows and 0 columns:
a = Array(Int, numrows, 0)

# Concatenate 3x1 arrays in your empty array:
for i in 1:numrows
b = [i, i+1, i*2] # Create the array you want to concatenate with a
a = hcat(a, b)
end

请注意,这里您知道数组 b具有 Int 类型的元素.因此我们可以创建数组 a具有相同类型的元素。

关于dataframe - 如何将向量添加到 Julia 中某个数组的列中?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/23839265/

28 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com