Sort Year-Month Column by Year and Month

Sort year-month column by year AND month

The as.yearmon() function (and the "yearmon" class) in package zoo is designed for this sort of data:

dat <- c("2009-Sep","2009-Feb","2009-Jan")
require(zoo)
d2 <- as.yearmon(dat, "%Y-%b")
> sort(d2)
[1] "Jan 2009" "Feb 2009" "Sep 2009"
> order(d2)
[1] 3 2 1
> d2[order(d2)]
[1] "Jan 2009" "Feb 2009" "Sep 2009"

You could of course paste0() a day onto each date and coerce to class "Date" via as.Date() but as.yearmon() seems more natural to me:

> as.Date(paste0(dat, "-01"), "%Y-%b-%d")
[1] "2009-09-01" "2009-02-01" "2009-01-01"

Note you can generate that same result by coercing the "yearmon" object to class "as.Date", e.g.:

> as.Date(d2)
[1] "2009-09-01" "2009-02-01" "2009-01-01"

How to sort the month_year column

Convert to yearmon class in which case they sort chronologically. At the end we convert back to character. Omit the last leg of the pipeline if leaving it as yearmon is ok.

library(zoo)

x |>
as.yearmon("%b-%Y") |>
sort(decreasing = TRUE) |>
format("%b-%Y")
## [1] "Mar-2021" "Feb-2021" "Jan-2021" "Dec-2020" "Nov-2020" "Oct-2020"
## [7] "Sep-2020" "Aug-2020" "Jul-2020" "Jun-2020" "May-2020" "Apr-2020"

Alternately write it like this:

format(sort(as.yearmon(x, "%b-%Y"), decreasing = TRUE), "%b-%Y")

or like this:

x[order(as.yearmon(x, "%b-%Y"), decreasing = TRUE)]

Sort by Month and Year in Dataframe

As @lmo has already mentioned that using just %m/%Y will not create a valid date. One solution is to use as.yearmon from zoo package.

library(zoo)    
df_prod<-df_prod[order(as.yearmon(df_prod$date,format="%m/%Y")),]

Sort dataframe columns by month and year

You can turn your column names to datetime, and then sort them:

df.columns = pd.to_datetime(df.columns, format='%b %y')

df = df[sorted(df.columns)]

>>> df
2018-01-01 2018-02-01 2018-03-01
Flavor
Vanilla 10.0 16.0 0.0
Chocolate 20.0 20.0 16.0

If you want your original string column names back simply add:

df.columns = df.columns.strftime('%b %y')

>>> df
Jan 18 Feb 18 Mar 18
Flavor
Vanilla 10.0 16.0 0.0
Chocolate 20.0 20.0 16.0

An alternative is to do it all in one line using sorted with a key (credit to @SpghttCd for the idea):

df[sorted(df.columns, key = lambda x: pd.to_datetime(x, format='%b %y'))]

Sort column names by month and year

Convert the column names to date object which you can easily order.

constant_cols <- 1:3
cols_to_sort <- names(asd)[-constant_cols]

cbind(asd[constant_cols],
asd[cols_to_sort[order(as.Date(paste0(cols_to_sort, '.01'), '%b.%Y.%d'))]])

This could be made a little shorter using zoo::as.yearmon.

cbind(asd[constant_cols], 
asd[cols_to_sort[order(zoo::as.yearmon(cols_to_sort, '%b.%Y'))]])

Sorting a Data Frame by Month with Repeating Years, based on Unique 'Other' Column

First convert values to ordered categoricals, so possible sorting by multiple columns in DataFrame.sort_values:

cat = ['January','February','March','April','May','June',
'July','August','September','October','November','December']
df['Month'] = pd.Categorical(df['Month'], ordered=True, categories=cat)
df = df.sort_values(['Item','Calendar Year','Month'])

Or create DatetimeIndex, so possible sorting by Item with datetimes:

df.index = pd.to_datetime(df['Calendar Year'] + df['Month'], format='%Y%B')
df = df.rename_axis('dt').sort_values(['Item','dt']).reset_index(drop=True)

Sorting Table 2 columns Month And Year

Try

Select * from MyTable ORDER BY Years, Months

When you say ORDER BY A, B, ordering by B will only be done for situations where A is the same. See the "ORDER BY Several Columns Example" in https://www.w3schools.com/sql/sql_orderby.asp.

But as @GMB notes in the other answer, you're probably better off converting to a date and ordering by that. Otherwise for any given year the months will be ordered alphabetically, rather than based on the actual calendar sequence.



Related Topics



Leave a reply



Submit