cjm2671
cjm2671

Reputation: 19466

How do I filter a pandas dataframe by the start of the index?

I have a dataframe of the form:

code    note    
OH2014  Oats Futures, March 2014, OH2014, CBOT
HGG2004 Copper Futures, February 2004, HGG2004, COMEX
BRH2014 Brazilian Real (BRL/USD) Futures, March 2014, ...
F5H2014 PJM PPL Zone Off-Peak Calendar-Month Day-Ahead...
PDMU2016    MISO Indiana Hub Day-Ahead Peak Calendar-Month...
GLG2015 Columbia Gulf Louisiana Natural Gas (Platts IF...
SSIZ2014    Coal (API 8) cfr South China (Argus/McCloskey)...
KCU1997 Coffee Futures, September 1997, KCU1997, ICE
FVH2003 5 Year Treasury Note Futures, March 2003, FVH2...
BOK1974 Soybean Oil Futures, May 1974, BOK1974, CBOT

where code is an index.

How can I filter it returning only entries where code begins with 'OH'?

Upvotes: 1

Views: 48

Answers (1)

John Zwinck
John Zwinck

Reputation: 249143

Something like this:

df[df.index.str.startswith('OH')]

Upvotes: 1

Related Questions