Morpheu5 Morpheu5 - 1 year ago 107
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)>
0 z0udgxc0lusu1gr4xj65
1 28080
2 28080
... ...

Answer Source
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 function, since this is what is used internally.

Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download