python
python

Reputation: 1940

ow can I use pandas to get the same group by result like mysql?

This is mysql table

I query it using "select * from link05m group by class, time", the result looks like: result from mysql

How can I get exactly the same result using pandas?

# -*- coding: utf-8 -*-

import pandas as pd
import sqlalchemy

conn = sqlalchemy.create_engine('mysql://root@localhost/test?charset=utf8')

link05m = pd.read_sql_query("select * from link05m where time ='2015-12-24 10:00:00'", conn)

grpResult = link05m.groupby(['time', 'class']) 

for r, g in grpResult:
    print g  # each g is make up of serveral Dataframes

Upvotes: 0

Views: 76

Answers (1)

knightofni
knightofni

Reputation: 1956

If you don't have aggregation to perform (ie : each pair of time, class is unique), then you don't need to do a pandas groupby. You should try :

link05m = pd.read_sql_query("select * from link05m where time ='2015-12-24 10:00:00'", conn)

Result = link05m.set_index(['time', 'class']) 

Upvotes: 1

Related Questions