Julia DataFrames, Insert new row at specific index

故事扮演 提交于 2019-12-11 17:46:46

问题


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

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])

in order to get

X
1
2
3
4

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.


回答1:


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.

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 │

Update

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

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


来源:https://stackoverflow.com/questions/51505007/julia-dataframes-insert-new-row-at-specific-index

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!