I have part of my code extracting an element from a column Ranks
by matching a string name
with elements in another column Names
:
rank = df.loc[df['Names'].str.contains(name), 'Ranks'].iloc[0]
The code is working as intended except for some few cases when name
contains parentheses.
For example, it will cause an error for name = Banana (1998)
.
I understand that str.contains might not be the best method here, but I have looked around and don't seem to have found any other post asking about the same problem so I can work my way from there.
A sample of the df can be reproduced with:
data = [['Apple', 10], ['Banana (1998)', 15], ['Banana (2000)', 14]]
df = pd.DataFrame(data, columns = ['Names', 'Ranks'])
If you use str.contains
, you need to escape '('
and ')'
in name
because they are special chars in regex as follows
name = 'Banana \(1998\)'
df['Names'].str.contains(name)
Out[655]:
0 False
1 True
2 False
Name: Names, dtype: bool
df.loc[df['Names'].str.contains(name), 'Ranks']
Out[659]:
1 15
Name: Ranks, dtype: int64
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