Sorry this seems like a really silly question but are dataframe[ ,-1]
and dataframe[-1]
the same, and does it work for all data types?
And why are they the same
By using equals() function we can directly check if df1 is equal to df2. This function is used to determine if two dataframe objects in consideration are equal or not. Unlike dataframe. eq() method, the result of the operation is a scalar boolean value indicating if the dataframe objects are equal or not.
Pandas library is heavily used for Data Analytics, Machine learning, data science projects, and many more. Pandas can load the data by reading CSV, JSON, SQL, many other formats and creates a DataFrame which is a structured object containing rows and columns (similar to SQL table).
Algorithm. Step 1: Define two Pandas series, s1 and s2. Step 2: Compare the series using compare() function in the Pandas series. Step 3: Print their difference.
DataFrame is a 2-dimensional labeled data structure with columns of potentially different types. You can think of it like a spreadsheet or SQL table, or a dict of Series objects. It is generally the most commonly used pandas object.
Almost.
[-1]
uses the fact that a data.frame is a list, so when you do dataframe[-1]
it returns another data.frame (list) without the first element (i.e. column).
[ ,-1]
uses the fact that a data.frame is a two dimensional array, so when you do dataframe[, -1]
you get the sub-array that does not include the first column.
A priori, they sound like the same, but the second case also tries by default to reduce the dimension of the subarray it returns. So depending on the dimensions of your dataframe
you may get a data.frame or a vector, see for example:
> data <- data.frame(a = 1:2, b = 3:4)
> class(data[-1])
[1] "data.frame"
> class(data[, -1])
[1] "integer"
You can use drop = FALSE
to override that behavior:
> class(data[, -1, drop = FALSE])
[1] "data.frame"
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