Reputation: 289
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
PASSWORD EMAIL AGE NAME FAVORITE_TEAM
Upvotes: 2
Views: 269
Reputation: 863166
Reason is because use set
s ({}
), 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