I have a simple script transforming data in a dataframe:
import pandas as pd
import numpy as np
df = pd.DataFrame({
'A':[123,None,456],
'B':[3698,598,None]})
def pad_value(item):
if item == None or item == np.nan:
return None
else:
return str(item).zfill(7)
df['A'] = df['A'].apply(lambda x: pad_value(x))
df['B'] = df['B'].apply(lambda x: pad_value(x))
The above seems to work fine. I have tried rewriting the last two lines to:
cols = ['A', 'B']
df[cols] = df[cols].apply(lambda x: pad_value(x))
However, this fails and gives a value error:
ValueError: The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().
- I am trying to understand why it can't be used in the above way.
- My pad_value function seems clunky - I wonder if there is a neater way of achieving the same?
Thanks