Converting a column of a DataFrame to a string

1,704 views
Skip to first unread message

Jason Solack

unread,
Mar 11, 2014, 9:35:58 PM3/11/14
to julia...@googlegroups.com
So this seems like silly question, but i'm looking to convert a column in a dataframe to a string and I don't see how to do that.

Any help?

Thank you!

Jason

John Myles White

unread,
Mar 11, 2014, 9:37:56 PM3/11/14
to julia...@googlegroups.com
I'm not totally sure what you mean. A column of a DataFrame is a vector, so one natural way to convert the column to a string would be to join all of the entries using join:

using DataFrames
df = DataFrame(A = 1:10)
join(df[:A], "::")

Is that what you're looking for?

-- John

Jason Solack

unread,
Mar 11, 2014, 9:48:31 PM3/11/14
to julia...@googlegroups.com
no, i'm sorry.  I have a vector that's eltype is Float64, it has values that i'd like to replace with the words that describe their values. right now when assigning them i'm getting errors since it's type is Float and it needs to be string.

so i'd like to convert the first column in the dataframe to an eltype of string so i can replace the numeric values with strings

John Myles White

unread,
Mar 11, 2014, 9:51:49 PM3/11/14
to julia...@googlegroups.com
Here's one way to do that: UTF8String[string(x) for x in df[:A]]

-- John

Jason Solack

unread,
Mar 12, 2014, 1:06:59 PM3/12/14
to julia...@googlegroups.com
that worked, thank you very much

David van Leeuwen

unread,
Mar 13, 2014, 4:13:45 AM3/13/14
to julia...@googlegroups.com
Hi, 

I've run into this and similar things many times, and somehow each time it takes me a lot of time to come up with such a solution again.  Don't ask me why I can't remember this, I must be getting old.  Would it make sense to define a

string(da::DataArray)

to do just that, within DataArrays.jl? 

John Myles White

unread,
Mar 13, 2014, 10:57:49 AM3/13/14
to julia...@googlegroups.com
There already is a string method, which does the same thing that string() does for the Array type that DataArray tries to mimic.

julia> da = @data([1, 2, 3])
3-element DataArray{Int64,1}:
1
2
3

julia> string(da)
"1\n2\n3\n”

In most cases, I’d advocate doing convert(Vector{UTF8String}, da), but that method fails for Array, so it should also fail for DataArray unless we change Base Julia.

— John

Jason Solack

unread,
Mar 13, 2014, 8:18:14 PM3/13/14
to julia...@googlegroups.com
I know the feeling, i get around hard to remember things like this by creating a snippet for it in Sublime text... that way I at least have a place to go to find them!
Reply all
Reply to author
Forward
0 new messages