Arrays Julia中的Query.jl能否从大小不同的数组中创建行?
我想知道如何使用Query.jl从数据帧中的数组中提取值,并将它们转换为单独的行 背景:我使用TextAnalysis.jl标记了一些文本,并希望有一个数据帧,每个标记一行,用于后续处理@map让我走了那么远,但尽管使用@mapmany进行了各种尝试,我仍然无法找到解决方案。也许@mapmany是这里的错误选择Arrays Julia中的Query.jl能否从大小不同的数组中创建行?,arrays,dataframe,julia,Arrays,Dataframe,Julia,我想知道如何使用Query.jl从数据帧中的数组中提取值,并将它们转换为单独的行 背景:我使用TextAnalysis.jl标记了一些文本,并希望有一个数据帧,每个标记一行,用于后续处理@map让我走了那么远,但尽管使用@mapmany进行了各种尝试,我仍然无法找到解决方案。也许@mapmany是这里的错误选择 example = DataFrame(line = 1:4, text = [["First", "line"], ["Then", "number", "two"], ["And",
example = DataFrame(line = 1:4, text = [["First", "line"], ["Then", "number", "two"], ["And", "numero", "tres"], ["The", "End"]])
我如何得到这样的结果:
line | word
-----------------
1 | First
1 | line
2 | Then
… | …
3 | tres
4 | The
4 | End
您可以使用重复操作和一些数组操作手动映射出该点
lines = [...]
flat_lines = vcat(lines...)
line_no = repeat(1:4, inner=2)
df = DataFrame(line=line_no, word=flat_lines)
您可以使用重复操作和一些数组操作手动映射出该点
lines = [...]
flat_lines = vcat(lines...)
line_no = repeat(1:4, inner=2)
df = DataFrame(line=line_no, word=flat_lines)
你应该做你想做的事:
julia> using DataFrames
julia> example = DataFrame(line = 1:4, text = [["First", "line"], ["Then", "number", "two"], ["And", "numero", "tres"], ["The", "End"]])
4×2 DataFrame
│ Row │ line │ text │
│ │ Int64 │ Array{String,1} │
├─────┼───────┼───────────────────────────┤
│ 1 │ 1 │ ["First", "line"] │
│ 2 │ 2 │ ["Then", "number", "two"] │
│ 3 │ 3 │ ["And", "numero", "tres"] │
│ 4 │ 4 │ ["The", "End"] │
julia> flatten(example, :text)
10×2 DataFrame
│ Row │ line │ text │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ First │
│ 2 │ 1 │ line │
│ 3 │ 2 │ Then │
│ 4 │ 2 │ number │
│ 5 │ 2 │ two │
│ 6 │ 3 │ And │
│ 7 │ 3 │ numero │
│ 8 │ 3 │ tres │
│ 9 │ 4 │ The │
│ 10 │ 4 │ End │
你应该做你想做的事:
julia> using DataFrames
julia> example = DataFrame(line = 1:4, text = [["First", "line"], ["Then", "number", "two"], ["And", "numero", "tres"], ["The", "End"]])
4×2 DataFrame
│ Row │ line │ text │
│ │ Int64 │ Array{String,1} │
├─────┼───────┼───────────────────────────┤
│ 1 │ 1 │ ["First", "line"] │
│ 2 │ 2 │ ["Then", "number", "two"] │
│ 3 │ 3 │ ["And", "numero", "tres"] │
│ 4 │ 4 │ ["The", "End"] │
julia> flatten(example, :text)
10×2 DataFrame
│ Row │ line │ text │
│ │ Int64 │ String │
├─────┼───────┼────────┤
│ 1 │ 1 │ First │
│ 2 │ 1 │ line │
│ 3 │ 2 │ Then │
│ 4 │ 2 │ number │
│ 5 │ 2 │ two │
│ 6 │ 3 │ And │
│ 7 │ 3 │ numero │
│ 8 │ 3 │ tres │
│ 9 │ 4 │ The │
│ 10 │ 4 │ End │