cjm2671
cjm2671

Reputation: 19486

Cleanest way to filter a Pandas dataframe?

Python:

pd.read_csv("CME-datasets-codes.csv", header=None)

Produces:

    0               1
0   CME/OH2014      Oats Futures, March 2014, OH2014, CBOT
1   CME/HGG2004     Copper Futures, February 2004, HGG2004, COMEX
2   CME/BRH2014     Brazilian Real (BRL/USD) Futures, March 2014, ...
3   CME/F5H2014     PJM PPL Zone Off-Peak Calendar-Month Day-Ahead...
4   CME/PDMU2016    MISO Indiana Hub Day-Ahead Peak Calendar-Month...

I want to filter this to show me all rows that start with "CME/C" in column 0.

What's the cleanest way?

Upvotes: 2

Views: 304

Answers (3)

Chris vCB
Chris vCB

Reputation: 1053

The easy and ugly route is

df[df['column_name'] == value]

At first sight, this can only catch equalities, but in reality, any vectorised function technically can be applied over this. As such, you can use:

df[df['column_name'].str.startswith('CME\/C')]

There are other methods, such as masking, that look a little more elegant. They are, however, neither more effective nor necessarily worth it. Sometimes, ugly is the way to go ;)

Upvotes: 1

user2285236
user2285236

Reputation:

df[df[0].str.startswith('CME/C')]

Upvotes: 8

Nils Gudat
Nils Gudat

Reputation: 13800

You can do:

df[df.0.str[:5]=="CME/C"]

Upvotes: 1

Related Questions