IcemanBerlin
IcemanBerlin

Reputation: 3437

Ordering Headers in a DataFrame using Python

How do I order the headers of a dataframe.

from pandas import *
import pandas
import numpy as np
df2 = DataFrame({'ISO':['DE','CH','AT','FR','US'],'Country': 
['Germany','Switzerland','Austria','France','United States']})
print df2

The result I get on default is this:

         Country ISO
0        Germany  DE
1    Switzerland  CH
2        Austria  AT
3         France  FR
4  United States  US

But I thought the ISO would be before the Country as that was the order i created it in the dataframe. It looks like it sorted it alphabetically?

How can i set up this simple table in memory to be used later in relational queries in my preferred column order. Everytime i reference the dataframe i dont want to have to order the columns.

My first coding post ever, ever.

Upvotes: 2

Views: 1600

Answers (2)

Zeugma
Zeugma

Reputation: 32095

a Python dict is unordered. The keys are not stored in the order you declare or append to it. The dict you give to the DataFrame as argument has an arbitrary order the DataFrame takes for granted.

You have several options to circumvent the issue:

  1. Use a OrderedDict object instead of the dict if you really need a dictionary as an input:

    df2 = DataFrame(OrderedDict([('ISO',['DE','CH','AT','FR','US']),('Country',['Germany','Switzerland','Austria','France','United States'])]))

  2. If you don't rely on a dictionary in the first place, then call the DataFrame with arguments declaring the columns:

    df2 = DataFrame({'ISO':['DE','CH','AT','FR','US'],'Country': ['Germany','Switzerland','Austria','France','United States']}, columns=['ISO', 'Country'])

Upvotes: 1

Wouter Overmeire
Wouter Overmeire

Reputation: 69156

A dict has no ordering, you can use columns argument to enforce one. If columns is not provided, default ordering is indeed alphabetically.

In [2]: df2 = DataFrame({'ISO':['DE','CH','AT','FR','US'],
   ...:                  'Country': ['Germany','Switzerland','Austria','France','United States']},
   ...:                  columns=['ISO', 'Country'])

In [3]: df2
Out[3]:
  ISO        Country
0  DE        Germany
1  CH    Switzerland
2  AT        Austria
3  FR         France
4  US  United States

Upvotes: 3

Related Questions