Issue
I'm trying to find if there is at least one similar item within all lists inside a Pandas dataframe row.
I have a table with columns 'store' and 'items'. Column'store' contains the list of lists with store ids, and the 'items' column represents the number of product items. I need to check whether all of these products are available in one store or at least in two of them.
I need to create the column 'flag' in which I check whether all of these lists in column 'store' contain at least one same value.
If they do, the flag will be 1.
If they don't, the flag will be 0.
For example, in the first row, all of the products are available in one store - 7, so the flag 1.
In the second row there are no matching values, so the flag will be 0. In the third row in list1 and list2 there is one common value - 92, but list3 lacks value 92, so the flag will be 0.
Store | Items | flag |
---|---|---|
[[7], [7], [7, 14], [7, 14]] |
4 | 1 |
[[67, 124], [30, 35, 38]] |
2 | 0 |
[[92, 147], [92], [53]] |
3 | 0 |
I've wrote this function, but it doest work even for the first case
def same_store(row):
if row['items'] == 1:
return 1
elif row['items'] > 1:
for list in row['store']:
if any(x in list for x in row['store']):
return 1
else:
return 0
Solution
If you want to check whether there is at least one item in common in all sublists per row, you can compute a set intersection and check if it's not empty:
df['flag'] = [1 if set.intersection(*map(set, l)) else 0 for l in df['Store']]
Or, if you need a more complex function that can also use the other columns (note that this is less efficient):
def same_store(row):
if len(set.intersection(*map(set, row['Store']))) > 0:
return 1
else:
return 0
df['flag'] = df.apply(same_store, axis=1)
Output:
Store Items flag
0 [[7], [7], [7, 14], [7, 14]] 1 1
1 [[67, 124], [30, 35, 38]] 2 0
2 [[92, 147], [86], [53]] 1 0
Reproducible input:
df = pd.DataFrame({'Store': [[[7], [7], [7, 14], [7, 14]],
[[67, 124], [30, 35, 38]],
[[92, 147], [86], [53]]],
'Items': [1, 2, 1],
})
Answered By - mozway
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.