How to order dataframe columns based on dtype?

Question:

I have a pandas DataFrame with a mixture of float64 and objects. I’d like to reorder the columns, so that all ints are at the start, and then all floats are at the end. Is there an easy way to do this?

Dataframe columns (currently):

Age Sex Weight BMI Job

Desired:

Age BMI Weight Sex Job 

(Age, BMI, Weight are all Int64, Sex, Job are objects)

My actual code has >100 similar features that I’d like to order this way

Asked By: Maya

||

Answers:

Let’s assume this example:

df = pd.DataFrame({'Age': [20],  'Sex': ['F'], 'Weight': [123],
                   'BMI': [12.3], 'Job': ['ABC']})
df.dtypes

Age         int64
Sex        object
Weight      int64
BMI       float64
Job        object
dtype: object

Interestingly the output of dtypes is a Series, so you can sort it and use the index to reindex your DataFrame:

out = df[df.dtypes.sort_values().index]

output:

   Age  Weight   BMI Sex  Job
0   20     123  12.3   F  ABC

For a custom order:

order = {np.dtype('int64'): 0,
         np.dtype('object'): 1,
         np.dtype('float64'): 2}

df[df.dtypes.map(order).sort_values().index]

output:

   Age  Weight Sex  Job   BMI
0   20     123   F  ABC  12.3
Answered By: mozway
Categories: questions Tags: , ,
Answers are sorted by their score. The answer accepted by the question owner as the best is marked with
at the top-right corner.