3

How can I reorder using pandas, 40,41,42,43,44 next to 2 ??
Is there any fastest and simple way to do it?
I dont want using type pd["sku_id","primary_category_code","primary_category_1","primary_category_2"...] to reorder...
take so much effort to do that.

For example:

     #   Column                     Non-Null Count  Dtype  
---  ------                     --------------  -----  
 0   sku_id                     274 non-null    object 
 1   primary_category_code      274 non-null    object 
 2   primary_category_name_chi  274 non-null    object 
 3   cat_codes                  274 non-null    object 
 4   cat_names_chi              274 non-null    object 
 5   name_chi                   274 non-null    object 
 6   brand_name_chi             274 non-null    object 
 7   summary_chi                274 non-null    object 
 8   description_en             86 non-null     object 
 9   description_chi            154 non-null    object 
 10  image_urls                 274 non-null    object 
 11  creation_time              274 non-null    object 
 12  store_code                 274 non-null    object 
 13  original_price             274 non-null    float64
 14  discount_price             271 non-null    float64
 15  stock_available            274 non-null    int64  
 16  out_of_stock_since         2 non-null      object 
 17  max_order_quantity         0 non-null      float64
 18  total_shipped_quantity     232 non-null    float64
 19  manu_country_chi           274 non-null    object 
 20  height                     274 non-null    float64
 21  length                     274 non-null    float64
 22  width                      274 non-null    float64
 23  dimension_unit             274 non-null    object 
 24  weight                     274 non-null    float64
 25  weight_unit                274 non-null    object 
 26  colors                     140 non-null    object 
 27  delivery_mode              274 non-null    object 
 28  pickup_days                274 non-null    object 
 29  num_days_to_be_ready       274 non-null    int64  
 30  online_date                0 non-null      float64
 31  offline_date               155 non-null    object 
 32  warranty_period            273 non-null    float64
 33  warranty_period_unit       270 non-null    object 
 34  warranty_supplier_en       270 non-null    object 
 35  warranty_supplier_chi      270 non-null    object 
 36  virtual_store_code         0 non-null      float64
 37  virtual_store_name_en      0 non-null      float64
 38  virtual_store_name_chi     0 non-null      float64
 39  primary_store              274 non-null    object 
 40  primary_category_1         274 non-null    object 
 41  primary_category_2         274 non-null    object 
 42  primary_category_3         274 non-null    object 
 43  primary_category_4         274 non-null    object 
 44  primary_category_5         274 non-null    object 

1 Answer 1

4

Given some DataFrame that looks like this:

df = pd.DataFrame(columns=range(1, 11))
df

Empty DataFrame
Columns: [1, 2, 3, 4, 5, 6, 7, 8, 9, 10]
Index: []

You can reorder the DataFrame's columns by index using iloc. I use np.r_ to make selection easy:

df.iloc[:, np.r_[0:2, 7:10, 2:8]]

Empty DataFrame
Columns: [1, 2, 8, 9, 10, 3, 4, 5, 6, 7, 8]
Index: []
Sign up to request clarification or add additional context in comments.

3 Comments

Actually, my columns are in wording not in numbers. Still can use it?
@JohnLee yes, .iloc looks at the numerical index of the columns. e.g., df.iloc[:, [2,3]] looks at the 3rd and 4th column, regardless of the column name (columns are counted 0, 1,2,3...).
@JohnLee yes, this solution is agnostic of column names as it is positional/index based solution. Thanks!

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.