Python Forum
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
str.endswith Output Query
#9
Hi there,

I am using the following line of Code :-

display.sort_values(by=['DATE'])
In another code, it correctly outputs the earliest days in the DataFrame Output first i.e. 10 before 15, but not in the month order I want :-

Output:
LOCATION DATE DAKOTA HURRICANE SPITFIRE MyIdx 176 Duxford 10-Jul-2004 D H S 177 Cirencester 10-Jul-2004 D H S 178 Brize Norton 10-Jul-2004 D H S 74 Shrivenham 20:00 10-Jun-2004 D H S 257 Campbletown 15-Aug-2004 D -- S 258 Sunderland 15-Aug-2004 D -- S 261 Scampton 15-Aug-2004 D -- S 200 RIAT Fairford 15-Jul-2004 D -- SS 22 Tilford 15-May-2004 D -- S 23 Abingdon 15-May-2004 D -- S 24 Hyde Heath Village 15-May-2004 D -- S
I want 10th June 2004 first then the 10th of July/s then the 15th of May's then the 15th of August Rows. How do I modify that line of Code, so that I can filter to get that order, without changing the index position of the Rows via code, which I know how to do ?

I mean add something to that line of code, so that the Earlier month with a day, is shown 'favoured' before the later month with the same day ? i.e. 10-Jun-2004 is shown before 10-Jul-2004 , 15-May-2004 is shown before 15-Jul-2004 Rows etc. But still dates with day 10 , showing before day 15 Rows.

(df3['DATE'].str.contains('-10$|15$'))
Was how I filtered the days.

Update :-

My Code now ends :-

display['DATE'] = pd.to_datetime(display['DATE'])

display = display.sort_values(by='DATE', key=lambda col: 100 * col.dt.day + col.dt.month)

display['DATE']= pd.to_datetime(display['DATE']).dt.strftime('%d-%b-%Y')

display
I was getting an TypeError, as I was using an early version of Pandas, and unfortunately you cant use sort_values with a key Argument, in versions of Pandas below v1.1. So I upgraded to the latest version, and now I get the correct desired DataFrame Output, when using those added lines of Code, when I run my Full Code.

Regards

Eddie Winch Smile
Reply


Messages In This Thread
str.endswith Output Query - by eddywinch82 - Aug-18-2020, 07:15 PM
RE: str.endswith Output Query - by buran - Aug-18-2020, 07:19 PM
RE: str.endswith Output Query - by eddywinch82 - Aug-18-2020, 07:30 PM
RE: str.endswith Output Query - by buran - Aug-18-2020, 08:09 PM
RE: str.endswith Output Query - by eddywinch82 - Aug-18-2020, 08:32 PM
RE: str.endswith Output Query - by buran - Aug-18-2020, 08:40 PM
RE: str.endswith Output Query - by eddywinch82 - Aug-18-2020, 09:28 PM
RE: str.endswith Output Query - by eddywinch82 - Aug-19-2020, 12:18 AM
RE: str.endswith Output Query - by eddywinch82 - Aug-21-2020, 11:59 PM

Possibly Related Threads…
Thread Author Replies Views Last Post
  Query output in Tuple paulo79 2 1,997 Apr-07-2022, 12:54 PM
Last Post: paulo79
  Format SQL Query Output phillyfa 2 4,079 Apr-22-2020, 07:45 AM
Last Post: buran
  syntax for endswith a digit birdieman 2 5,672 Mar-04-2017, 07:21 PM
Last Post: birdieman

Forum Jump:

User Panel Messages

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