Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Select column of dataframe with name matching a string in Julia?

I have a large DataFrame that I import from a spreadsheet. I have the names of several columns that I care about in an array of strings. How do I select a column of the DataFrame who's name matches the contents of a string? I would have though that something like this would work

using DataFrames
df = DataFrame(A = 1:4, B = ["M", "F", "F", "M"], C = 2:5)
colsICareAbout = [":B" ":C"]
df[:A] #This works
df[colsICareAbout[1]] #This doesn't work

Is there a way to do this?

like image 967
ARM Avatar asked Mar 25 '15 16:03

ARM


People also ask

How do I select column names in a DataFrame?

You can get the column names from pandas DataFrame using df. columns. values , and pass this to python list() function to get it as list, once you have the data you can print it using print() statement.

What is a DataFrame in Julia?

DataFrame is a 2 dimensional mutable data structure, that is used for handling tabular data. Unlike Arrays and Matrices, a DataFrame can hold columns of different data types. The DataFrames package in Julia provides the DataFrame object which is used to hold and manipulate tabular data in a flexible and convenient way.


1 Answers

Strings are different than symbols, but they're easy to convert.

colsICareAbout = ["B","C"]
df[symbol(colsICareAbout[1])]

Mind you it might be better to make the entries in colsICareAbout symbols to begin with, but I don't know where your data is coming from.

like image 134
Sebastian Good Avatar answered Sep 29 '22 05:09

Sebastian Good