I'm trying to delete a row from a data frame in which each row has a name. I cannot use indexes to delete the rows, only it's name. I have this dataframe:
DF<- data.frame('2014' = c(30,20,4, 50), '2015' = c(25,40,6, 65), row.names = c("mobile login", "computer login","errors", "total login"))
I've tried
DF["mobile login",] <- NULL
and
DF <- DF[-"mobile login",]
and more combinations with no results.
What can I do? Thanks
PS: The last row is the sum of the first two (there are other in the real DF, that's only an example), and once they are added, I don't need them, only the result, the "total login" value.
In general, the rows are removed by using the row index number but we can do the same by using row names as well. This can be done by storing the row names that should be removed in a vector and then removing through subsetting with single square brackets as shown in the below examples.
You can use the drop function to delete rows and columns in a Pandas DataFrame.
You cannot actually delete a row, but you can access a data frame without some rows specified by negative index. This process is also called subsetting in R language. A Big Note: You should provide a comma after the negative index vector -c().
Use %in%
along with an appropriate subset of your data frame. To remove the rows named errors
and mobile login
you can use the following code:
row.names.remove <- c("errors", "mobile login")
> DF[!(row.names(DF) %in% row.names.remove), ]
X2014 X2015
computer login 20 40
total login 50 65
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With