Python Forum
How can I do it in Python for this SQL statement?
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How can I do it in Python for this SQL statement?
#1
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!
Reply
#2
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()
Reply


Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020