Pandas Slicing

Jupyter Data
1 min readNov 30, 2015

--

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'])

Sign up to discover human stories that deepen your understanding of the world.

Free

Distraction-free reading. No ads.

Organize your knowledge with lists and highlights.

Tell your story. Find your audience.

Membership

Read member-only stories

Support writers you read most

Earn money for your writing

Listen to audio narrations

Read offline with the Medium app

--

--

Jupyter Data
Jupyter Data

Written by Jupyter Data

Fastest way to Explore your Data

No responses yet

Write a response