Python Forum
Ordering of pandas DataFrame - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Ordering of pandas DataFrame (/thread-24307.html)



Ordering of pandas DataFrame - new_to_python - Feb-08-2020

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?


RE: Ordering of pandas DataFrame - perfringo - Feb-08-2020

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.


RE: Ordering of pandas DataFrame - new_to_python - Feb-08-2020

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?


RE: Ordering of pandas DataFrame - new_to_python - Mar-15-2020

Hi, anybody knows the answer?


RE: Ordering of pandas DataFrame - jefsummers - Mar-15-2020

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


RE: Ordering of pandas DataFrame - new_to_python - Mar-15-2020

Thank you.