Вот предложение для трех функций:
import pandas as pd
# first we create the data
data = pd.DataFrame({'name': ['A','B','C','D','E'],
'ids': ['147 616 813','51 616 13 813','776','51 671 13 813
1092','13 404 492 903 1093']})
def func1(num, series):
# num must be an int
# series a Pandas series
tx = series.apply(lambda x: True if str(num) in x.split() else False)
output_list = series.index[tx].tolist()
return output_list
def func2(num, series):
# num must be an int
# series a Pandas series
series = series.iloc[func1(num, series)]
series = series.apply(lambda x: x.split()).tolist()
output_list = set([item for sublist in series for item in sublist])
output_list.remove(str(num))
return list(output_list)
def func3(num1,num2,series):
# num1 must be an int
# num2 must be an int
# series a Pandas series
if str(num1) in func2(num2, series):
num1_index = func1(num1, series)
num2_index = func1(num2, series)
return list(set(num1_index) & set(num2_index))
else:
return 'no match'
, а затем вы можете проверить их:
func1(13, data['ids'])
func2(13, data['ids'])
func3(13,51,data['ids'])