Issue
I have a pandas data frame which doesn't have an index yet (just artificial 1,2,3,.. index)
Column 'store', 'style' is string, column 'color', 'size' is a long int. None of them are unique by themselves, but the concatenation of them are unique.
I want to concatenate them to produce an index, but
df2['store']+df2['style']+str(df2['color'])+str(df2['size'])
or
df2['store']+df2['style']+df2['color'].to_string()+df2['size'].to_string()
both doesn't work. I think it takes the whole column, force it to become a string and concatenate which results in weird symbols. And merges doesn't work correctly.
What's the correct way to concatenate a string column and a long column?
Solution
This should be:
df2['store'] + df2['style'] + df2['color'].astype(str) + df2['size'].astype(str)
Explanation: str(df2['size'])
will make a string representation of the full column (one string, comparable as to what you see if you print the string), while .astype(str)
will convert all values of the series to strings.
to_string
gives the same result as str()
(but takes optional parameters to control the result)
Answered By - joris
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.