Matthaeus Gaius Caesar
Matthaeus Gaius Caesar

Reputation: 130

What is the Python equivalent for the R function names( )?

The function names() in R gets or sets the names of an object. What is the Python equivalent to this function, including import?

Usage:

names(x)

names(x) <- value

Arguments:

(x) an R object.

(value) a character vector of up to the same length as x, or NULL.

Details:

Names() is a generic accessor function, and names<- is a generic replacement function. The default methods get and set the "names" attribute of a vector (including a list) or pairlist.

Continue R Documentation on Names( )

Upvotes: 1

Views: 4389

Answers (3)

surya ambati
surya ambati

Reputation: 104

In Python (pandas) we have .columns function which is equivalent to names() function in R:

Ex:

# Import pandas package  
import pandas as pd  

# making data frame  
data = pd.read_csv("Filename.csv")  

# Extract column names
list(data.columns) 

Upvotes: 3

Sam Mason
Sam Mason

Reputation: 16194

not sure if there is anything directly equivalent, especially for getting names. some objects, like dicts, provide .keys() method that allows getting things out

sort of relevant are the getattr and setattr primitives, but it's pretty rare to use these in production code

I was going to talk about Pandas, but I see user2357112 has just pointed that out already!

Upvotes: 1

user2357112
user2357112

Reputation: 281585

There is no equivalent. The concept does not exist in Python. Some specific types have roughly analogous concepts, like the index of a Pandas Series, but arbitrary Python sequence types don't have names for their elements.

Upvotes: 0

Related Questions