How can I do it in Python for this SQL statement? - Printable Version +- Python Forum (https://python-forum.io) +-- Forum: Python Coding (https://python-forum.io/forum-7.html) +--- Forum: Data Science (https://python-forum.io/forum-44.html) +--- Thread: How can I do it in Python for this SQL statement? (/thread-20641.html) |
How can I do it in Python for this SQL statement? - mmk1995 - Aug-23-2019 Hello All, I would like to ask, I have dataframe like this: datetime,NO,PG,PART_TIME,COURSE_YEAR 09/02/2018 13:43:42,3035447142,UG,F,2 09/02/2018 13:43:45,3035447142,UG,F,2 09/02/2018 13:43:53,3035533288,UG,F,1 09/02/2018 13:43:53,3035447142,UG,F,2 09/02/2018 13:43:55,3035535169,UG,F,1 ........... ........... I would like to do a SQL statement: select min(a.datetime),NO,COURSE_YEAR from this_table a group by NO,COURSE_YEAR; How should I do it in python3.7 (using pandas)? Thank you very much! RE: How can I do it in Python for this SQL statement? - scidam - Aug-24-2019 I think the following example should help you: df = pd.DataFrame({'x': [3,3,0,0,0,1,2,1], 'y': [10,10,10,11,11,3,3,3], 'z':['a', 'a', 'a', 'a', 'b', 'b','b', 'b']}) df.groupby(['y', 'z']).x.min().reset_index()Note, you should be sure that the datetime column is of 'date'-type (not a string). Hope the following will work: df.groupby(['NO', 'COURSE_YEAR'])['datetime'].min().reset_index() |