Random Dataframe Column ordering

Viewed 68

strange question here but I was trying to create an empty dataframe with the following code. I want the columns to be in the order that I wrote them but when output they are in a different order. Is there a reason why this is happening intuitively?

import pandas as pd

user_df = pd.DataFrame(columns={'NAME',
                                'AGE',
                                'EMAIL',
                                'PASSWORD',
                                'FAVORITE_TEAM'
                               })
user_df

Output:

PASSWORD    EMAIL   AGE NAME    FAVORITE_TEAM
1 Answers

Reason is because use sets ({}), there is not defined order.

Docs:

A set object is an unordered collection of distinct hashable objects.

If use list ([]) all working nice:

user_df = pd.DataFrame(columns={'NAME',
                                'AGE',
                                'EMAIL',
                                'PASSWORD',
                                'FAVORITE_TEAM'
                               })
print (user_df)
Empty DataFrame
Columns: [AGE, FAVORITE_TEAM, EMAIL, NAME, PASSWORD]
Index: []

user_df = pd.DataFrame(columns=['NAME',
                                'AGE',
                                'EMAIL',
                                'PASSWORD',
                                'FAVORITE_TEAM'
                               ])
print (user_df)
Empty DataFrame
Columns: [NAME, AGE, EMAIL, PASSWORD, FAVORITE_TEAM]
Index: []
Related