Julia DataFrames,在特定索引处插入新行

Julia DataFrames,在特定索引处插入新行

问题描述:

是否可以在特定索引处向现有数据框添加一行?

Is there a way to add a row to an existing dataframe at a specific index?

例如您有一个包含3行1列的数据框

E.g. you have a dataframe with 3 rows and 1 columns

df = DataFrame(x = [2,3,4])

X
2
3
4

任何执行以下操作的方法:

any way to do the following:

insert!(df, 1, [1])

为了获得

X
1
2
3
4

我知道我可能可以合并两个数据帧df = [df1; df2],但我希望每当要插入一行时都避免对大型DF进行垃圾处理.

I know that i could probably concat two dataframes df = [df1; df2] but i was hoping to avoid garbaging a large DF whenever i want to insert a row.

我想您想就地进行.然后,您可以像这样使用insert!函数:

I guess you want to do it in place. Then you can use insert! function like this:

julia> df = DataFrame(x = [1,2,3], y = ["a", "b", "c"])
3×2 DataFrames.DataFrame
│ Row │ x │ y │
├─────┼───┼───┤
│ 1   │ 1 │ a │
│ 2   │ 2 │ b │
│ 3   │ 3 │ c │

julia> foreach((v,n) -> insert!(df[n], 2, v), [4, "d"], names(df))

julia> df
4×2 DataFrames.DataFrame
│ Row │ x │ y │
├─────┼───┼───┤
│ 1   │ 1 │ a │
│ 2   │ 4 │ d │
│ 3   │ 2 │ b │
│ 4   │ 3 │ c │

当然,您必须确保添加的集合中的列数正确.

Of course you have to make sure that you have the right number of columns in the added collection.

如果您接受使用DataFrame的未导出内部结构,则可以做得更简单:

If you accept using unexported internal structure of a DataFrame you can do it even simpler:

julia> df = DataFrame(x = [1,2,3], y = ["a", "b", "c"])
3×2 DataFrames.DataFrame
│ Row │ x │ y │
├─────┼───┼───┤
│ 1   │ 1 │ a │
│ 2   │ 2 │ b │
│ 3   │ 3 │ c │

julia> insert!.(df.columns, 2, [4, "d"])
2-element Array{Array{T,1} where T,1}:
 [1, 4, 2, 3]
 String["a", "d", "b", "c"]

julia> df
4×2 DataFrames.DataFrame
│ Row │ x │ y │
├─────┼───┼───┤
│ 1   │ 1 │ a │
│ 2   │ 4 │ d │
│ 3   │ 2 │ b │
│ 4   │ 3 │ c │

更新

因为Julia 1.0 df.columns无法正常工作.而是写:

Since Julia 1.0 df.columns does not work. Instead write:

insert!.(eachcol(df, false), 2, [4, "d"])