Home > Back-end >  Dynamically change order of column headers with dates next to other fixed columns
Dynamically change order of column headers with dates next to other fixed columns

Time:08-10

I do have a merged dataframe with columns in the wrong order. d =[col_A, col_B, date_1, date_2, date_3, date_4, date_5, date_6, date_7, date_8 , date_9, col_C, col_D, date_10, col_F]

intended order: d = [col_C, col_A, col_B, col_D, col_E, date_1, date_2, date_3, date_4, date_5, date_6, date_7, date_8, date_9, date_10, col_F]

the "col" columns stay the same, but the "date" columns change every day. How could I adapt the order ? I used d = d.iloc[:,[12, 0, 1, 13, 14, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 15, 16]], which is a bad option for me in case one date column is missing.

CodePudding user response:

df
###
  ColA ColB ColC ColD      date1      date2      date3      date4 ColE
0   a1   b1   c1   d1 2020-01-01 2021-01-01 2022-01-01 2023-01-01   e1
1   a2   b2   c2   d2 2020-01-02 2021-01-02 2022-01-02 2023-01-02   e2
2   a3   b3   c3   d3 2020-01-03 2021-01-03 2022-01-03 2023-01-03   e3

df.info()
###
<class 'pandas.core.frame.DataFrame'>
RangeIndex: 3 entries, 0 to 2
Data columns (total 9 columns):
 #   Column  Non-Null Count  Dtype         
---  ------  --------------  -----         
 0   ColA    3 non-null      object        
 1   ColB    3 non-null      object        
 2   ColC    3 non-null      object        
 3   ColD    3 non-null      object        
 4   date1   3 non-null      datetime64[ns]
 5   date2   3 non-null      datetime64[ns]
 6   date3   3 non-null      datetime64[ns]
 7   date4   3 non-null      datetime64[ns]
 8   ColE    3 non-null      object        
dtypes: datetime64[ns](4), object(5)
memory usage: 344.0  bytes
orders_ordinal = [4,0,1,5,2,3,6,7,8]
orders_nominal = ['date1','ColA','ColB','date2','ColC','ColD','date3','date4','ColE']

Ordinal ( integer position based )

df.iloc[:,orders_ordinal]
###
       date1 ColA ColB      date2 ColC ColD      date3      date4 ColE
0 2020-01-01   a1   b1 2021-01-01   c1   d1 2022-01-01 2023-01-01   e1
1 2020-01-02   a2   b2 2021-01-02   c2   d2 2022-01-02 2023-01-02   e2
2 2020-01-03   a3   b3 2021-01-03   c3   d3 2022-01-03 2023-01-03   e3

Nominal ( label based )

df.loc[:,orders_nominal]
###
       date1 ColA ColB      date2 ColC ColD      date3      date4 ColE
0 2020-01-01   a1   b1 2021-01-01   c1   d1 2022-01-01 2023-01-01   e1
1 2020-01-02   a2   b2 2021-01-02   c2   d2 2022-01-02 2023-01-02   e2
2 2020-01-03   a3   b3 2021-01-03   c3   d3 2022-01-03 2023-01-03   e3

CodePudding user response:

You can dynamic extract columns names not in first and last list:

first = ['col_C', 'col_A', 'col_B','col_D', 'col_E']
last = ['col_F']

dates = [x for x in d if x not in first   last]
print (dates)
['date_1', 'date_2', 'date_3', 'date_4', 'date_5', 
 'date_6', 'date_7', 'date_8 ', 'date_9', 'date_10']

And last join in your order with filer by subset:

df = df[first   dates   last]
  • Related