Python Forum
Ordering of pandas DataFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ordering of pandas DataFrame
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:

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.
I'm not 'in'-sane. Indeed, I am so far 'out' of sane that you appear a tiny blip on the distant coast of sanity. Bucky Katt, Get Fuzzy

Da Bishop: There's a dead bishop on the landing. I don't know who keeps bringing them in here. ....but society is to blame.
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.

Possibly Related Threads…
Thread Author Replies Views Last Post
  update values in one dataframe based on another dataframe - Pandas iliasb 2 367 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 478 Jun-22-2021, 07:57 PM
Last Post: snippsat
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 811 Jan-11-2021, 09:53 PM
Last Post: Azureaus
  Comparing results within a list and appending to pandas dataframe Aryagm 1 649 Dec-17-2020, 01:08 PM
Last Post: palladium
  How to search for specific string in Pandas dataframe Coding_Jam 1 879 Nov-02-2020, 09:35 AM
Last Post: PsyPy
  PANDAS: DataFrame | White Spaces & Special Character Removal traibr 1 1,490 Sep-10-2020, 07:02 PM
Last Post: eddywinch82
  No Output In Pandas DataFrame Query eddywinch82 1 807 Aug-17-2020, 09:25 PM
Last Post: eddywinch82
  strange error from pandas dataframe djf123 1 1,863 Jul-27-2020, 05:25 AM
Last Post: scidam
  Pandas DataFrame not updating HelpMePlease 3 1,058 Jul-11-2020, 07:19 PM
Last Post: jefsummers
  Pandas DataFrame visual Truman 8 1,409 Jul-10-2020, 06:11 AM
Last Post: hussainmujtaba

Forum Jump:

User Panel Messages

Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020