Reputation: 5190
I'm given a task of converting an R script to Python. I'm trying to understand the functionality.
It is mentioned as below in the script.
#get id for single_triad
id <- names(forecast_object_list[single_triad_index])
forecast_object_list is something that is returned from R's by() function. I'm not sure if it's going to be a list or dataframe in R.
single_triad_index is an integer counter in a for loop.
What does this names() function actually do?
I don't have access to databases that are used in the R script to execute and look at the format of data.
After some research, I understood that it is returning headers in a dataframe. http://www.endmemo.com/program/R/names.php
Does that mean I can use pandas's df.columns to get the column names from a dataframe like:
list(my_dataframe.columns.values)
Are these two equal? or am I assuming it wrong?
Upvotes: 1
Views: 376
Reputation:
names()
returns the names of an object's elements. For example, if we call names
on each of the following elements R will return [1] "a" "b"
:
ve <- c(a = 1, b = 2) # atomic vector
li <- list(a = 1, b = 2) # list
df <- data.frame(a = 1, b = 2) # data frame
For your purposes it would worth knowing that data frames are just a special kind of list - is.list(li)
and is.list(df)
will both return TRUE
. That means that, depending on what you are doing, you frequently don't need to differentiate between these two data objects. That said, sometimes there are differences, e.g. while names()
works for both data frames and lists, colnames()
will only work for data frames (and matrices, etc.). It ultimately depends on the available attributes.
If we compare the above to Python then the result seems to be the same. I think the main difference is that names()
is a generic accessor that can be used with multiple data object types, while .columns
is a specific attribute of the Pandas DataFrame
object.
df = DataFrame({'a':[1], 'b':[2]})
df.columns.to_list()
# ['a', 'b']
Upvotes: 1