1

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')

2 Answers 2

5

If this actually what you want to do, a faster alternative is np.select():

cond=[df<= 10,df > 10]
choice=['<= 10','> 10' ]
df[:]=np.select(cond,choice)
print(df)

   colA1  colA2  colA3
0  <= 10   > 10   > 10
1  <= 10  <= 10   > 10
2  <= 10   > 10   > 10
3   > 10   > 10   > 10
4  <= 10  <= 10  <= 10

You can also try with df.applymap() for your function:

df[['colA1','colA2','colA3']].applymap(function)
#df.applymap(function)

   colA1  colA2  colA3
0  <= 10   > 10   > 10
1  <= 10  <= 10   > 10
2  <= 10   > 10   > 10
3   > 10   > 10   > 10
4  <= 10  <= 10  <= 10
Sign up to request clarification or add additional context in comments.

1 Comment

Also don't use dict as dictionary names since it overwrites the builtin python dict func.
1

this should do it

df.apply(lambda x: pd.Series([function(x['colA1']),function(x['colA2']),function(x['colA3'])]), axis=1).rename({0:'colA1',1:'colA2',2:'colA3'}, axis=1)

Output

   colA1  colA2  colA3
0  <= 10   > 10   > 10
1  <= 10  <= 10   > 10
2  <= 10   > 10   > 10
3   > 10   > 10   > 10
4  <= 10  <= 10  <= 10

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.