Matt_Davis
Matt_Davis

Reputation: 289

Random Dataframe Column ordering

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

Upvotes: 2

Views: 269

Answers (1)

jezrael
jezrael

Reputation: 863166

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: []

Upvotes: 2

Related Questions