Python Forum

Full Version: Ordering of pandas DataFrame
You're currently viewing a stripped down version of our content. View the full version with proper formatting.
Hello, I am new to pandas.

data = {'state': ['Ohio', 'Ohio', 'Ohio', 'Nevada', 'Nevada', 'Nevada'], 
    ...:         'year': [2000, 2001, 2002, 2001, 2002, 2003], 
    ...:         'pop': [1.5, 1.7, 3.6, 2.4, 2.9, 3.2]}  

frame = pd.DataFrame(data)        
When I typed: frame

I got the list:

Out[41]:
state year pop
0 Ohio 2000 1.5
1 Ohio 2001 1.7
2 Ohio 2002 3.6
3 Nevada 2001 2.4
4 Nevada 2002 2.9
5 Nevada 2003 3.2

However, somebody else got the DataFrame with the headings in the order of: pop followed by state and then year. How come the order of the columns are different? Is it due to the version of pandas use? Does the order of the columns of DataFrame matter in pandas? How do I reorder the way the columns are displayed?
One possibility is that this is not about pandas version but Python version.

Starting from Python 3.6 dictionaries ('data' in your code) are insertion ordered. Prior to that Python dictionaries were unordered. So if somebody uses pandas with Python < 3.6 then there is no guarantee that order will be same.
Thanks. I am using Python 3.7.4. So the way the columns are displayed is determined by the order of insertion. Besides the way the columns are displayed, does the order of insertion or ordering in general, affects anything else?
Hi, anybody knows the answer?
It should not, as long as you are referencing by labels (column name and row name). And, this is an argument for that approach. You can also address a cell by its row and column numbers, and obviously then it matters. AFAIK, the order does not limit you
Thank you.