I have a df which I need to transform column from object to string and then count how many characters each string has. So I made a generic function shown below:
def get_cpf(cpf):
if cpf.astype(pd.StringDtype()).str.len() == 11:
return 'CPF'
elif (cpf.astype(pd.StringDtype()).str.len() < 10 or cpf.astype(pd.StringDtype()).str.len() > 14) or (cpf.astype(pd.StringDtype()).str.len() > 11 and cpf.astype(pd.StringDtype()).str.len() < 13):
return 'Sem identificacao'
else:
return 'CNPJ'
When I apply the function in my df it returns the following error:
df['check6'] = get_cpf(df['CPF_ou_CNPJ'])
Error:
ValueError Traceback (most recent call last)
~\AppData\Local\Temp\ipykernel_16256\4021734166.py in <module>
----> 1 df['check6'] = get_cpf(df['CPF_ou_CNPJ'])
~\AppData\Local\Temp\ipykernel_16256\2308362031.py in get_cpf(cpf)
1 def get_cpf(cpf):
2 # cpf = cpf.fillna('0')
----> 3 if cpf.astype(pd.StringDtype()).str.len() == 11:
4 return 'CPF'
5 elif (cpf.astype(pd.StringDtype()).str.len() < 10 or cpf.astype(pd.StringDtype()).str.len() > 14) or (cpf.astype(pd.StringDtype()).str.len() > 11 and cpf.astype(pd.StringDtype()).str.len() < 13):
~\Anaconda3\lib\site-packages\pandas\core\generic.py in __nonzero__(self)
1536 def __nonzero__(self):
1537 raise ValueError(
-> 1538 f"The truth value of a {type(self).__name__} is ambiguous. "
1539 "Use a.empty, a.bool(), a.item(), a.any() or a.all()."
1540 )
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
Can anyone help me?