look for a matching row in a column containing multiple values

Asked 2 years ago, Updated 2 years ago, 31 views

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.

Enter a description of the image here

python python3

2022-09-29 22:29

2 Answers

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(','))]


2022-09-29 22:29

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)])


2022-09-29 22:29

If you have any answers or tips


© 2024 OneMinuteCode. All rights reserved.