Home > Enterprise >  How to keep columns in the same sequence while transposing the dataframe in Pandas?
How to keep columns in the same sequence while transposing the dataframe in Pandas?

Time:04-12

I have a dataframe df with the columns

cycle_end_date | trigger | deliver | cost

when i do df.transpose()

I get the following result

        cycle_end_date_1 | cycle_end_date_2 | cycle_end_date_3
cost
deliver
trigger

which is fine but how do i preserve the sequence of my index? It should come in the same sequence that is trigger, deliver, cost

Any idea?

CodePudding user response:

Using .loc should let you reorder the indexes:

df.loc[['cost', 'deliver', 'trigger']]
  • Related