8

I am updating a data frame using apply of function.

But now I need to modify multiple columns using this function,

Here is my sample code:

def update_row(row):
    listy = [1,2,3]
    return listy

dp_data_df[['A', 'P','Y']] = dp_data_df.apply(update_row, axis=1)

It is throwing the following error:

ValueError: shape mismatch: value array of shape (10,) could not be broadcast to indexing result of shape (3,10)

Thanks in advance.

0

2 Answers 2

21

You can return pd.Series:

dp_data_df = pd.DataFrame({'A':[3,5,6,6],
                           'B':[6,7,8,9],
                           'P':[5,6,7,0],
                           'Y':[1,2,3,4]})
print (dp_data_df)
   A  B  P  Y
0  3  6  5  1
1  5  7  6  2
2  6  8  7  3
3  6  9  0  4

def update_row(row):
    listy = [1,2,3]
    return pd.Series(listy)

dp_data_df[['A', 'P','Y']] = dp_data_df.apply(update_row, axis=1)
print (dp_data_df)
   A  B  P  Y
0  1  6  2  3
1  1  7  2  3
2  1  8  2  3
3  1  9  2  3
Sign up to request clarification or add additional context in comments.

3 Comments

@jezrael is there a way that I could change column names for each record and still use apply?
@Ahsan - Not sure if understand, how is possible change columns names?
@jezrael how can I use a condition for the update? For example, I want to update the row if the col B = 8 or 9.
3

You can use zip your output:

def update_row(row):
    listy = [1,2,3]
    return listy

dp_data_df['A'], dp_data_df['P'], dp_data_df['Y'] = zip(*dp_data_df.apply(update_row, axis=1))

2 Comments

Very elegant solution! Do you mind explaining how it works?
This solution avoids the numeric return and allows the return of the char function. It works fine and is what I was looking for, but I don't understand how it works.

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.