Dataframes.jl: delete a row

Created on 3 May 2014  路  17Comments  路  Source: JuliaData/DataFrames.jl

How can I do it? I found deleterows, but it is not exported. And the argument to deleterows is not straightforward - requiring rows to keep instead of rows to delete. Can I have something simply like: deleterows(df, 3), which 3 is the row to delete?

Most helpful comment

For those reading this with modern versions of Julia, the @milktrader approach is now:
df[[1:2; 4:end],:]

All 17 comments

Found an answer on the net, and so cooked up a function for this:

function deleterow!(df::DataFrame, row::Int)
inds = trues(nrow(df))
inds[row] = false
df = df[inds, :]
end

Sorry, the above function won't pass back the modified df. I have to do the following instead - return a new df. Can anyone please give an easier way?

function deleterow(df::DataFrame, row::Int)
inds = trues(nrow(df))
inds[row] = false
return df[inds, :]
end

df[[1:2, 4:end],:]

Thanks, that is really simple. Didn't know one can combine [[:,:],:] that way.

We should probably have functions called deleterows! and deletecols!.

Code golf?

deleterows!(df::DataFrame,x::Int) =  df[[1:size(df,1)][1:size(df,1).!=x],:]

It would more verbose, but for performance reasons we might want to use splice!. First we'd need to implement it for DataVectors, though.

72 characters is par though :disappointed:

Personally I think such methods should be put into a different package, something like DataFramesTools, because the existing getindex methods seem sufficient.

I don't think it makes sense to ask users to install a separate package just to delete a row in a convenient fashion. We're going to get questions all the time by people who can't find deleterows!.

Ok, good point. A separate package is probably a bad idea.

Thanks for handling this, Simon.

For those reading this with modern versions of Julia, the @milktrader approach is now:
df[[1:2; 4:end],:]

Write the line of code to permanently delete rows three, five, nine from a DataFrame with the name df?
can anyone tell what will be the answer?

@Madhuri97 First congratulations for having reached this issue. You are close to obtain your answer, but this seems an exam question and the precise final answer to the specific question should be you to provide it. Good luck!

@Madhuri97 First congratulations for having reached this issue. You are close to obtain your answer, but this seems an exam question and the precise final answer to the specific question should be you to provide it. Good luck!

Yes, this is an exam question which I have attempted using many syntaxes but still, this shows error

My answer:
df.deleterows!([3,5,9])
deleterows!(df,[3,5,9])
deleterows([3,5,9])

error Message: Make use of the bang form of the function and pass the row numbers as an array (inside the square bracket, separated by commas). Do not include any spaces in the code.

@Madhuri97 the correct answer is:

if you want to do it in place:

delete!(df, [3,5,9])

if you want a new data frame:

df[Not([3,5,9]), :]

if you want a view:

view(df, Not([3,5,9]), :)

@Madhuri97 the correct answer is:

if you want to do it in place:

delete!(df, [3,5,9])

if you want a new data frame:

df[Not([3,5,9]), :]

if you want a view:

view(df, Not([3,5,9]), :)

Thank you very much it worked

Was this page helpful?
0 / 5 - 0 ratings

Related issues

abieler picture abieler  路  7Comments

gustafsson picture gustafsson  路  6Comments

garborg picture garborg  路  8Comments

cossio picture cossio  路  5Comments

CameronBieganek picture CameronBieganek  路  6Comments