Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Most elegant way to select rows by a string value

Tags:

python

pandas

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():
like image 459
codecreed Avatar asked Aug 04 '19 12:08

codecreed


3 Answers

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)]
like image 117
anky Avatar answered Nov 17 '22 06:11

anky


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.

like image 2
Willem Van Onsem Avatar answered Nov 17 '22 07:11

Willem Van Onsem


Or use:

frame[frame['Description'].str.contains('(?i)on wallet exchange')]
like image 2
U12-Forward Avatar answered Nov 17 '22 06:11

U12-Forward