I'm facing an unexpected behavior with base R.
I want to retrieve the first row of a dataframe with its colnames as a vector, and this common method works perfectly for most cases.
df = data.frame(A=c(12,13), B=c(24,25))
unlist(df[1,]) #class=numeric
# A B
# 12 24
But when the dataframe is only composed of one column, the result is coerced to an unnamed vector:
df = data.frame(A=c(12,13))
unlist(df[1,]) #class=numeric too
# 12
How to keep the name is the second case?
You can get the first row with iloc[0] and the last row with iloc[-1] . If you want to get the value of the element, you can do with iloc[0]['column_name'] , iloc[-1]['column_name'] .
Get the First Row of Pandas using iloc[] Using the Pandas iloc[] attribute we can get the single row or column by using an index, by specifying the index position 0 we can get the first row of DataFrame. iloc[0] will return the first row of DataFrame in the form of Pandas Series.
You can use the duplicated() function to find duplicate values in a pandas DataFrame.
When extracting a dataframe using [
by default drop
argument is TRUE
.
From ?Extract
drop - If TRUE the result is coerced to the lowest possible dimension.
Also you can check the class for both the dataframe after extracting the row.
df1 = data.frame(A=c(12,13), B=c(24,25))
df2 = data.frame(A=c(12,13))
class(df1[1, ])
#[1] "data.frame"
class(df2[1, ])
#[1] "numeric"
As we can see df2
is coerced to a vector. Using drop = FALSE
will keep it as dataframe and not drop the dimensions.
df2[1,, drop = FALSE]
# A
#1 12
class(df[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