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():
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)]
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.
Or use:
frame[frame['Description'].str.contains('(?i)on wallet exchange')]
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With