Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to get the first row of a dataframe with names when there is only one column? [duplicate]

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?

like image 981
Dan Chaltiel Avatar asked Nov 21 '19 09:11

Dan Chaltiel


People also ask

How do I extract the first row of a data frame?

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'] .

How do I get the first row value of a DataFrame?

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.

How do you find duplicate rows in a data frame?

You can use the duplicated() function to find duplicate values in a pandas DataFrame.


1 Answers

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"
like image 113
Ronak Shah Avatar answered Sep 17 '22 14:09

Ronak Shah