0

I have the following code that.

import pandas as pd 


def generate_list(row): 
    return [row['A']*2, row['B']*3] 

def main(): 
    data = { 
            'A':[0, 2, 3],  
            'B':[4, 15, 6]} 
      
    df = pd.DataFrame(data) 
   
    # applying function to each row in 
    df['C'], df['D'] = df.apply(lambda row: generate_list(row), axis=1) 
       
if __name__ == '__main__': 
    main() 

The code simply applies a function to each row of my df and the results returned as a list. I am trying to add each element of my returned list to two new columns. However, I get the following error:

df['C'], df['D'] = df.apply(lambda row: generate_list(row), axis=1) 
ValueError: too many values to unpack (expected 2)
1
  • a quick way is df[['C', 'D']] = df.apply(lambda row: generate_list(row), axis=1).tolist() Commented Feb 3, 2021 at 5:59

3 Answers 3

1

Replace:

    df['C'], df['D'] = df.apply(lambda row: generate_list(row), axis=1) 

With:

    df[['C','D']] = pd.DataFrame(df.apply(lambda row: generate_list(row), axis=1).tolist(), index= df.index)
    return df

Gives you:

A   B   C   D
0   0   4   0   12
1   2   15  4   45
2   3   6   6   18
Sign up to request clarification or add additional context in comments.

Comments

0

Because apply() function can't return a list or tuple, it just return a Series or a DataFrame, so it's OK when you add only one new column but not for more than one new columns. For your purpose, you can use concat function like this:

import pandas as pd 

def generate_list(row): 
    return [row['A']*2, row['B']*3] 

def main(): 
    data = { 
            'A':[0, 2, 3],  
            'B':[4, 15, 6]} 
      
    df = pd.DataFrame(data) 
   
    # applying function to each row in 
    df = pd.concat([df, df.apply(lambda row: generate_list(row), axis=1, result_type='expand').rename(columns={0:'C',1:'D'})],axis=1)
    print(df) 
       
if __name__ == '__main__': 
    main() 

The result of above code is:

   A   B  C   D
0  0   4  0  12
1  2  15  4  45
2  3   6  6  18

Is that what you want?

Comments

0

I try some experiments, like:

data = {'A':[0, 2, 3],  
        'B':[4, 15, 6]} 
df = pd.DataFrame(data) 
def generate_list(row): 
    return [row['A']*2, row['B']*3] 

obj = df.apply(lambda row: generate_list(row), axis=1)

    0    [0.0, 12.0]
    1    [4.0, 45.0]
    2    [6.0, 18.0]
    dtype: object

obj_accept = np.array(obj.sum()).reshape(-1,2)

    array([[ 0., 12.],
           [ 4., 45.],
           [ 6., 18.]])

# it's worked
df[['C', 'D']] = obj_accept

# also work
df[['C', 'D']] = np.array(obj.tolist())

# also work
df[['C', 'D']] = obj.tolist()

# not worked
df[['C', 'D']] = obj

the shapes:

obj.values.shape              # (3,)
df[['C', 'D']].shape          # (3, 2)
np.array(obj.tolist()).shape  # (3, 2)

some weird result:

df = pd.DataFrame({'A':[0, 2, 3], 'B':[4, 15, 6]} ) 
df.loc[:1, ['C', 'D']] = obj.loc[:1]
print(obj.loc[:1])

    0    [0.0, 12.0]
    1    [4.0, 45.0]
    dtype: object

print(df)

       A   B     C     D
    0  0   4   0.0   4.0
    1  2  15  12.0  45.0
    2  3   6   NaN   NaN

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.