Morpheu5 Morpheu5 - 2 months ago 30
Ruby Question

How do I address Daru Vectors in a DataFrame using symbols rather than strings?

I see this happening in a lot of online code examples, but when I parse my CSV file, all I get are strings as column indices, like

> data = Daru::DataFrame.from_csv('my_fancy_data.csv')
> data[:user_id]
IndexError: Specified index :user_id does not exist

> data['user_id']
=> #<Daru::Vector(42815)>
user_id
0 z0udgxc0lusu1gr4xj65
1 28080
2 28080
... ...

Answer
data = Daru::DataFrame.from_csv('my_fancy_data.csv', headers: true, header_converters: :symbol)

From the documentation:

You can specify all the options to the .from_csv function that you do to the Ruby CSV.read() function, since this is what is used internally.