How to transform a sql output into a SFrame?

1

I have an output of an SQL query in this way:

[{'count': 8L, 'subscriber_id': 54, 'eclipse_id': 11294}, {'count': 1L, 'subscriber_id': 54, 'eclipse_id': 11243}, {'count': 1L, 'subscriber_id': 54, 'eclipse_id': 11295}, {'count': 1L, 'subscriber_id': 150, 'eclipse_id': 11313}, {'count': 1L, 'subscriber_id': 150, 'eclipse_id': 11305}, {'count': 7L, 'subscriber_id': 150, 'eclipse_id': 11309}, {'count': 1L, 'subscriber_id': 150, 'eclipse_id': 11267}]

in this way:

({'subscriber_id':["54","54","54","150","150","150"],
'eclipse_id':["11295","11305",,""11309,"11313","11309","11267"],
'count':[8,1,1,1,7,1]})

To use econ graphlab in a SFrame:

data = graphlab.SFrame({'subscriber_id':["54","54","54","150","150","150"],
    'eclipse_id':["11295","11305",,""11309,"11313","11309","11267"],
    'count':[8,1,1,1,7,1]})

Can this transformation help me?

Actually the output of the SQL query is quite longer.

    
asked by ThePassenger 22.06.2017 в 15:13
source

1 answer

1

If you want to create a SFrame with columns count | eclipse_id | subscriber_id you can pass the list of dictionaries directly to the SFrame constructor and then use the unpack method:

import graphlab as gl

datos = [{'count': 8L, 'subscriber_id': 54, 'eclipse_id': 11294}, {'count': 1L, 'subscriber_id': 54, 'eclipse_id': 11243}, {'count': 1L, 'subscriber_id': 54, 'eclipse_id': 11295}, {'count': 1L, 'subscriber_id': 150, 'eclipse_id': 11313}, {'count': 1L, 'subscriber_id': 150, 'eclipse_id': 11305}, {'count': 7L, 'subscriber_id': 150, 'eclipse_id': 11309}, {'count': 1L, 'subscriber_id': 150, 'eclipse_id': 11267}]
data = gl.SFrame(datos).unpack('X1', column_name_prefix="")

Exit:

>>> data
Columns:
  count   int
  eclipse_id  int
  subscriber_id   int

Rows: 7

Data:
+-------+------------+---------------+
| count | eclipse_id | subscriber_id |
+-------+------------+---------------+
|   8   |   11294    |       54      |
|   1   |   11243    |       54      |
|   1   |   11295    |       54      |
|   1   |   11313    |      150      |
|   1   |   11305    |      150      |
|   7   |   11309    |      150      |
|   1   |   11267    |      150      |
+-------+------------+---------------+
[7 rows x 3 columns]
    
answered by 22.06.2017 / 16:32
source