In [6]:
import seaborn as sns
import pandas as pd
%matplotlib inline
pd.options.display.max_rows = 10
data = sns.load_dataset("titanic")

Changing the position/ order of the columns in your table

I want to make this as simple as possible, let's take a look at the data below and see where columns are.

In [7]:
data.head()
Out[7]:
survived pclass sex age sibsp parch fare embarked class who adult_male deck embark_town alive alone
0 0 3 male 22.0 1 0 7.2500 S Third man True NaN Southampton no False
1 1 1 female 38.0 1 0 71.2833 C First woman False C Cherbourg yes False
2 1 3 female 26.0 0 0 7.9250 S Third woman False NaN Southampton yes True
3 1 1 female 35.0 1 0 53.1000 S First woman False C Southampton yes False
4 0 3 male 35.0 0 0 8.0500 S Third man True NaN Southampton no True

I am going to take columns age , sibsp and parch from the middle and move them all the way to then end. to do this I used the loc method, I convered some theory about loc in my filtering tutorial. loc method is a powerful method and you can do and should do so much with it.

You can use the loc method to..
  1. Move your columns arround and change the order of the columns
  2. add new columns
  3. filter your data , rows and columns
  4. ...
In [9]:
data.loc[:,['survived', 'pclass', 'sex', 'fare',
            'embarked', 'class', 'who', 'adult_male', 'deck', 'embark_town',
            'alive', 'alone', 'age', 'sibsp', 'parch']  ]
Out[9]:
survived pclass sex fare embarked class who adult_male deck embark_town alive alone age sibsp parch
0 0 3 male 7.2500 S Third man True NaN Southampton no False 22.0 1 0
1 1 1 female 71.2833 C First woman False C Cherbourg yes False 38.0 1 0
2 1 3 female 7.9250 S Third woman False NaN Southampton yes True 26.0 0 0
3 1 1 female 53.1000 S First woman False C Southampton yes False 35.0 1 0
4 0 3 male 8.0500 S Third man True NaN Southampton no True 35.0 0 0
... ... ... ... ... ... ... ... ... ... ... ... ... ... ... ...
886 0 2 male 13.0000 S Second man True NaN Southampton no True 27.0 0 0
887 1 1 female 30.0000 S First woman False B Southampton yes True 19.0 0 0
888 0 3 female 23.4500 S Third woman False NaN Southampton no False NaN 1 2
889 1 1 male 30.0000 C First man True C Cherbourg yes True 26.0 0 0
890 0 3 male 7.7500 Q Third man True NaN Queenstown no True 32.0 0 0

891 rows × 15 columns

Let's recap on what the loc method is doing above, just to eradicate any confusion there might be.

so we have df.loc[]
loc stands for location and the first argument is the row location followed by a comma and then the columns location.
if you don't pass in the comma it will assume it's the row location.
df.loc[rowlocation , columnslocation]
rowlocation and column location can be list of rows and columns, slices of rows and columns labels to use slices for raw index use iloc

In [ ]: