Bottom Page

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.
new_to_python likes this post
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?

Top Page

Possibly Related Threads...
Thread Author Replies Views Last Post
  Obtaining Correct Date In Pandas DataFrame eddywinch82 14 1,066 Feb-17-2020, 11:45 AM
Last Post: eddywinch82
  Parse XML String in Pandas Dataframe creedX 2 457 Dec-09-2019, 07:35 PM
Last Post: creedX
  Pandas dataframe to join three tables using like condition among them sandeep_ganga 0 286 Nov-29-2019, 08:30 AM
Last Post: sandeep_ganga
  Pandas Dataframe to Google Big Query Ecniv 2 968 Nov-21-2019, 02:26 PM
Last Post: Ecniv
  manipulating a dataframe - pandas nsx200 2 274 Nov-14-2019, 10:38 AM
Last Post: nsx200
  Pandas dataframe columns collapsed in Spyder when printing UniKlixX 2 308 Nov-04-2019, 07:00 AM
Last Post: UniKlixX
  pandas dataframe iloc mystery edvvardbrian 2 359 Oct-29-2019, 02:55 PM
Last Post: jefsummers
  How to add a few empty rows into a pandas dataframe python_newbie09 2 1,847 Sep-20-2019, 08:52 AM
Last Post: python_newbie09
  Dropping a column from pandas dataframe marco_ita 6 2,125 Sep-07-2019, 08:36 AM
Last Post: marco_ita
  created a pandas series instead of pandas DataFrame ibaad1406 6 845 Sep-06-2019, 06:23 AM
Last Post: ibaad1406

Forum Jump:

Users browsing this thread: 1 Guest(s)