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?
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.
To select a single column, use square brackets [] with the column name of the column of interest.
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.
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.
Try this:
names(results) <- make.names( names(results) )
That's the same function that gets applied when read.table
and its descendants are used.
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")
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]
).
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