codecreed
codecreed

Reputation: 61

Most elegant way to select rows by a string value

Is there a more elegant way to write this code:

df['exchange'] = frame.loc[frame['Description'].str.lower().str.contains("on wallet exchange")]

The .str twice seems ugly.

When I iterate over the entire dataframe row by row, I can use:

if "on wallet exchange" in row['Description'].casefold():

Upvotes: 6

Views: 63

Answers (3)

U13-Forward
U13-Forward

Reputation: 71580

Or use:

frame[frame['Description'].str.contains('(?i)on wallet exchange')]

Upvotes: 2

willeM_ Van Onsem
willeM_ Van Onsem

Reputation: 476813

You can use the case=False parameter for the str.contains(..) method:

frame.loc[frame['Description'].str.contains('on wallet exchange', case=False)]

Note that .casefold() and .lower() are not equivalent, these have some special cultural rules, for example the eszett ß [wiki] maps on 'ss', since it is considered to be equivalent with 'SS'. Pandas has a str.casefold(..) method to map on case-invariant folds of the original string.

Upvotes: 2

anky
anky

Reputation: 75080

Use case=False , also add na=False to be safe so if the series contains either numerics(@ jezrael-Thank you ) or NaN , this will be evaluated as False

frame.loc[frame['Description'].str.contains("on wallet exchange",case=False,na=False)]

Upvotes: 6

Related Questions