I would like to extract rows that exactly match the criteria from a table with multiple values separated by commas as shown in the attachment.
I have tried implementation using "str.contains" and "in", but for example, I want to extract only records (a,b) containing 17, but c and d containing 17 are also extracted, which is troubling me.
Please let me know if you know a good way.
How to list and find out
import pandas as pd
df = pd.DataFrame({'product': ['a', 'b', 'c', 'd'],
'column_a': ['17, 117, 217', '17', '117', '217']})
df[df['column_a'].map(lambdax:'17' in x.split(','))]
import pandas as pd
df = pd.DataFrame({
US>'product': ['a', 'b', 'c', 'd',
'column_a': ['17, 117, 217', '17', '117', '217',
})
print(df[df['column_a'].str.contains(r'\b17\b')])
#
product column_a
0a 17,117,217
1 b 17
print (df[df['column_a'].str.split(', ').map({'17'.issubset)])
© 2024 OneMinuteCode. All rights reserved.