-1
The code I used to create the column is returning with the following error:
The Truth value of a Series is ambiguous. Use a.Empty, a.bool(), a. item(), a.any() or a.all().
I don’t know how to fix this mistake.
import pandas as pd
import numpy as np
raw_data = {'regiment': ['Nighthawks', 'Nighthawks', 'Nighthawks', 'Nighthawks'],
'company': ['1st', '1st', '2nd', '2nd'],
'deaths': ['kkk', 52, '25', 616],
'battles': [5, 42, 2, 2],
'size': ['l', 'll', 'l', 'm']}
df = pd.DataFrame(raw_data, columns = ['regiment', 'company', 'deaths', 'battles', 'size'])
def valida_CEP(x):
if x < 9:
return '0' + str(x)
else:
return str(x)
df['batles_comzero'] = df.apply(valida_CEP(df['battles']),axis=1)
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
I am using a notebook jupyter and your example returned the following error: C: Users Antonio Anaconda3 lib site-Packages ipykernel_main_.py:2: Settingwithcopywarning: A value is trying to be set on a copy of a Slice from a Dataframe See the caveats in the Documentation: http://pandas.pydata.org/pandas-docs/stable/indexing.html#Indexing-view-versus-copy from ipykernel import kernelapp as app.
– Antonio
Try using the following expression: df['batles_comzero'] = df.battles.apply(valida_CEP)
– Teodimiro Matsena