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; [email protected]
- 2; 54321; 87778885566; [email protected]
- 3; 98765; 87776664577; [email protected]
- 4; 66678; 87778885566; [email protected]
- 5; 34567; 84547895566; [email protected]
- 6; 34567; 89087545678; [email protected]
The output should be:
- 2; 54321; 87778885566; [email protected]
- 4; 66678; 87778885566; [email protected]
- 5; 34567; 84547895566; [email protected]
- 6; 34567; 89087545678; [email protected]
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 [email protected]
3 66678 87778885566 [email protected]
4 34567 84547895566 [email protected]
5 34567 89087545678 [email protected]
Answered By - onyambu
0 comments:
Post a Comment
Note: Only a member of this blog may post a comment.