ikel ikel - 3 months ago 14
Python Question

graphlab create sframe combine two column

I have two columns with strings. let's say col1 and col2
now how can we combine the content of col1 and col2 into col3 with graphlab SFrame?

col1 col2
23 33
42 11
........


into

col3
23,33
42,11
....


unstack will only give sarray or dict, i only one a bag of words

tried

user_info['X5']=user_info['X3'].apply(lambda x:x+','+user_info['X4'].apply(lambda y:y))


doesnt seem to be right

any idea?

Answer

Using pandas:

In [271]: df
Out[271]: 
   col1  col2
0    23    33
1    42    11

In [272]: df['col3'] = (df['col1'].map(str) + ',' + df['col2'].map(str))

In [273]: df
Out[273]: 
   col1  col2   col3
0    23    33  23,33
1    42    11  42,11

Using graphlab:

In [17]: sf
Out[17]: 
Columns:
    col1    int
    col2    int

Rows: 2

Data:
+------+------+
| col1 | col2 |
+------+------+
|  23  |  33  |
|  42  |  11  |
+------+------+
[2 rows x 2 columns]

In [18]: sf['col3'] = sf['col1'].apply(str) + ',' + sf['col2'].apply(str)

In [19]: sf
Out[19]: 
Columns:
    col1    int
    col2    int
    col3    str

Rows: 2

Data:
+------+------+-------+
| col1 | col2 |  col3 |
+------+------+-------+
|  23  |  33  | 23,33 |
|  42  |  11  | 42,11 |
+------+------+-------+
[2 rows x 3 columns]
Comments