I need to detect and count how many patterns from UF_med variable are in UF_cadastral variable.
That is my dataset:
df = {'id': [1,2,3],
'UF_med':[['SP', 'SC', 'PA'], ['SP'], ['AM', 'RJ', 'PA', 'RS']],
'UF_cadastral': [['SP', 'PA'], ['SP'], ['AM', 'RS']]}
df = pd.DataFrame(df)
df.head()
Although I need to count the patterns, I tried at least detect one pattern. However, the code only detect the first pattern of UF_med variable. I used that code:
df['Detect_Municipio'] = df.apply(lambda x: x['UF_med'] in x['UF_cadastral'], axis=1)
The result should be like that:
df = {'id': [1,2,3],
'UF_med':[['SP', 'SC', 'PA'], ['SP'], ['AM', 'RJ', 'PA', 'RS']],
'UF_cadastral': [['SP', 'PA'], ['SP'], ['AM', 'RS']],
'Detect_Municipio':[2,1,2]}
df = pd.DataFrame(df)