Issue
I need to write a function.
It takes any value from the dataset as input and should look for an intersection in all rows.
For example: phone = 87778885566
The table is represented by the following fields:
- key
- id
- phone
Test data:
- 1; 12345; 89997776655; test@gmail.com
- 2; 54321; 87778885566; two@gmail.com
- 3; 98765; 87776664577; three@gmail.com
- 4; 66678; 87778885566; four@gmail.com
- 5; 34567; 84547895566; four@gmail.com
- 6; 34567; 89087545678; five@gmail.com
The output should be:
- 2; 54321; 87778885566; two@gmail.com
- 4; 66678; 87778885566; four@gmail.com
- 5; 34567; 84547895566; four@gmail.com
- 6; 34567; 89087545678; five@gmail.com
It should check all values and if values intersect somewhere, return a dataset with intersections.
Solution
You could use recurssion
:
import numpy as np
def relation(dat, values):
d = dat.apply(lambda x: x.isin(values.ravel()))
values1 = dat.iloc[np.unique(np.where(d)[0]),:]
if set(np.array(values)) == set(values1.to_numpy().ravel()):
return values1
else:
return relation(dat, values1.to_numpy().ravel())
relation(df.astype(str), np.array(['87778885566']))
1 2 3
1 54321 87778885566 two@gmail.com
3 66678 87778885566 four@gmail.com
4 34567 84547895566 four@gmail.com
5 34567 89087545678 five@gmail.com
Answered By - onyambu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.