I am trying to update values in a couple of columns,
dt = pd.DataFrame([["value1","600M"],["value2","70K"]])
dt.columns = ['val', 'som']
I want to update 600M to 600000000 and 70K to 70000. For this, I wrote a function,
def convert_unites(val_to_convert,conversion = 'numeric'):
if(conversion == 'numeric'):
units_numeric = dict([
('K', 1000),
('M', 1000000),
('B', 1000000000)
])
if(val_to_convert.str.contains('K')):
return int(val_to_convert.str.replace('K',''))* units_numeric['K']
elif(val_to_convert.str.contains('M')):
return int(val_to_convert.str.replace('M',''))* units_numeric['M']
else:
return 0
Calling the function
dt.apply(lambda x: (convert_unites(x)) if x.name in ['som'] else x, axis=0)
I got this Error
ValueError: ('The truth value of a Series is ambiguous. Use a.empty,
a.bool(), a.item(), a.any() or a.all().', 'occurred at index som')