Python Forum
Ordering of pandas DataFrame
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Ordering of pandas DataFrame
#1
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?
Reply
#2
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.
Reply
#3
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?
Reply
#4
Hi, anybody knows the answer?
Reply
#5
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
Reply
#6
Thank you.
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  HTML Decoder pandas dataframe column mbrown009 3 961 Sep-29-2023, 05:56 PM
Last Post: deanhystad
  Use pandas to obtain cartesian product between a dataframe of int and equations? haihal 0 1,088 Jan-06-2023, 10:53 PM
Last Post: haihal
  Pandas Dataframe Filtering based on rows mvdlm 0 1,396 Apr-02-2022, 06:39 PM
Last Post: mvdlm
  Pandas dataframe: calculate metrics by year mcva 1 2,266 Mar-02-2022, 08:22 AM
Last Post: mcva
  Pandas dataframe comparing anto5 0 1,240 Jan-30-2022, 10:21 AM
Last Post: anto5
  PANDAS: DataFrame | Replace and others questions moduki1 2 1,757 Jan-10-2022, 07:19 PM
Last Post: moduki1
  PANDAS: DataFrame | Saving the wrong value moduki1 0 1,525 Jan-10-2022, 04:42 PM
Last Post: moduki1
  update values in one dataframe based on another dataframe - Pandas iliasb 2 9,094 Aug-14-2021, 12:38 PM
Last Post: jefsummers
  empty row in pandas dataframe rwahdan 3 2,417 Jun-22-2021, 07:57 PM
Last Post: snippsat
Question Pandas - Creating additional column in dataframe from another column Azureaus 2 2,913 Jan-11-2021, 09:53 PM
Last Post: Azureaus

Forum Jump:

User Panel Messages

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