Pandas Slicing
I found the loc, iloc, reindex and a multitude of methods to go about slicing and dicing a dataframe. Here are the various methods that seem to work:
To have a sample dataset — lets use one from seaborn
# Load dataset
import seaborn.apionly as sns
titanic = sns.load_dataset('titanic')
The dataset contains the following columns [‘survived’,‘pclass’, ‘sex’, ‘age’, ‘sibsp’, ‘parch’, ‘fare’, ‘embarked’, ‘class’, ‘who’, ‘adult_male’, ‘deck’, ‘embark_town’, ‘alive’, ‘alone’]
# Get all columns in data
>> list(titanic.columns)
Its got a numeric index going from 0 to 890
# get row indices
>> titanic.index# get rows and columns
>> titanic.shape
Select all rows but just the three columns [‘sex’, ‘age’, ‘fare’]
# using the column names
>> titanic.loc[:,['sex','age','fare']]
or
# using the column indices
>> titanic.iloc[:,[2,3,6]]# using the ix (Note: you can use the (),[] interchangeably)
>> titanic.ix[:,(‘sex’,’age’,’fare’)]
or
>> titanic.ix[:,(2,3,6)]# using the reindex method
>> titanic.reindex(columns=['sex','age','fare'])