I am attempting to write a function and apply it to multiple fields in a pandas dataframe. The function takes column colA1, and assigns a value to a new column, colB2 based on conditional statements. This function works if a single column is given, e.g. colA1, but how could I write it to iterate through a list of columns, returning a corresponding number of new columns?
The following function works on a single column:
dict = {'colA1':[2,6,8,28,5],
'colA2': [38,6,14,63,3],
'colA3':[90,40,80,98,3]}
df = pd.DataFrame(dict)
def function(x):
if x <= 10:
return '<= 10'
elif x > 10:
return '> 10'
df['colB1']=df['colA1'].apply(function)
df['colB1']
This returns:
0 <= 10
1 <= 10
2 <= 10
3 > 10
4 <= 10
I attempted to apply it to multiple columns as shown here: Update Multiple Columns using Pandas Apply Function
df[['colB1', 'colB2', 'colB3']]=df[['colA1', 'colA2', 'colA3']].apply(function)
But this returns: ValueError: ('The truth value of a Series is ambiguous. Use a.empty, a.bool(), a.item(), a.any() or a.all().', 'occurred at index colA1')