Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Cannot access column of data frame

Tags:

r

I have some data in a results 3-column data frame when I print results I see:

 results

TIMESTAMP                 SYMBOL_NAME         "t.price"
1 2014-10-17 14:00:00       GOOG                   400.25

Notice the "" around the t.price column

When I go to access the t.price column like it comes back null.

 results$t.Price
 NULL

when I do

  names(results)   

I see

[1] "TIMESTAMP"        "SYMBOL_NAME"        "\"t.PRICE\"" 

Can you tell me what is going on and why the "" is appearing or how I can access t.price?

like image 907
user3022875 Avatar asked Oct 17 '14 22:10

user3022875


People also ask

How do I view a column in a data frame?

You can use the loc and iloc functions to access columns in a Pandas DataFrame. Let's see how. If we wanted to access a certain column in our DataFrame, for example the Grades column, we could simply use the loc function and specify the name of the column in order to retrieve it.

How do I get certain columns from a data frame?

To select a single column, use square brackets [] with the column name of the column of interest.

How do I see a specific column in Pandas?

This is the most basic way to select a single column from a dataframe, just put the string name of the column in brackets. Returns a pandas series. Passing a list in the brackets lets you select multiple columns at the same time.

How do you access data frame elements?

By using loc and iloc We can access a single row and multiple rows of a DataFrame with the help of “loc” and “iloc”. Access a single row or multiple rows by name.


3 Answers

Try this:

names(results) <- make.names( names(results) )

That's the same function that gets applied when read.table and its descendants are used.

like image 66
IRTFM Avatar answered Oct 21 '22 08:10

IRTFM


Whatever the source of your data frame is, it appears that the t.Price column is surrounded by quotation marks. These in turn are escaped by \ as \" as otherwise "" would be the end of a string of 0 length.

Methods to work around this are suggested by the comments, you can access this by the index of the column or by renaming the columns, which can be done by assigning to names.

names(results) <- c("name1", "name2", "name3")
like image 35
Hans Avatar answered Oct 21 '22 08:10

Hans


You can remove the extra quotes with gsub

results
#             TIMESTAMP SYMBOL_NAME "t.price"
# 1 2014-10-17 14:00:00        GOOG    400.25
results$t.price
# NULL
names(results)[3] <- gsub('\"', "", names(results)[3])
results$t.price
# [1] 400.25

Just to be safe, you might want to run it on all the names of the data set (just remove both [3]).

like image 32
Rich Scriven Avatar answered Oct 21 '22 08:10

Rich Scriven