How to delete several rows from a dataframe in one command

Hi,

I would like to delete several rows from a dataframe in one command. For example:

deleteat!(df, [row1, row2, row3])

Where row(n) is a variable and is given by the user. I can delete one row with readline():

if choice == "3"
print("\033c")
println("Which row to delete?")
dr = readline()
deleteat!(df, parse(Int64,dr))
CSV.write("FILE.csv", df)
println("Data deleted...")
println(df)
println("\n")
end

I tried nearly everything to delete multiple rows at once in one command, but without succes.
Is this possible in Julia?

I think, from what you posted, that your trouble is that you need to parse multiple input rows as an array, which you can do by broadcasting parse (with a dot):

julia> rows = readline()
1 2 3
"1 2 3"

julia> rows
"1 2 3"

julia> parse.(Int, split(rows))
3-element Vector{Int64}:
 1
 2
 3

julia> df = DataFrame(A = 1:5, B = 6:10)
5Γ—2 DataFrame
 Row β”‚ A      B     
     β”‚ Int64  Int64 
─────┼──────────────
   1 β”‚     1      6
   2 β”‚     2      7
   3 β”‚     3      8
   4 β”‚     4      9
   5 β”‚     5     10

julia> deleteat!(df, parse.(Int, split(rows)))
2Γ—2 DataFrame
 Row β”‚ A      B     
     β”‚ Int64  Int64 
─────┼──────────────
   1 β”‚     4      9
   2 β”‚     5     10
2 Likes

Thank you lmiq, that did the trick! I didn’t realize i had to put a β€œdot” after β€œparse”. I searched for half a day but didn’t realize that this was solution for my case. I will dive a bit more into the dot operator know ;-).