2

I have a simple dataframe:

import pandas as pd
d = pd.DataFrame({'a':[[1], [2], [3]], 'b': [[4], [5], [6]]}) 
print d
  a    b
  0  [1]  [4]
  1  [2]  [5]
  2  [3]  [6]

And I want to get

  combined
 0   [1, 4]
 1   [2, 5]
 2   [3, 6]

I used the following

d['combined'] = d.apply(lambda row: row.a + row.b, axis=1)

and it give me error:

ValueError: Wrong number of items passed 2, placement implies 1

why would

d['combined'] = d.apply(lambda row: row.a[0] + row.b[0], axis=1)

would work(although not what I need),but my code above gives error?

Update:

actually I my original code was more like this:

d = pd.DataFrame({'a':[[1, 2], [2, 3], [3, 4]], 'b': [[4, 5], [5, 6], [6, 7]]}) 
        a       b
0  [1, 2]  [4, 5]
1  [2, 3]  [5, 6]
2  [3, 4]  [6, 7]

and I want to get the first element of a and last element in b in a list as

     combined
0    [1, 5]
1    [2, 6]
2    [3, 7]

It seems that I can just create a new column with empty string first and use the apply to modify it, as pointed out by one of the answers below, directly adding the column won't give me flexibility to manipulate the list.

user3230153
  • 123
  • 3
  • 11

3 Answers3

6

Just create a blank series name 'combined' as column first, I dont know why though ValueError: Wrong number of items passed - Meaning and suggestions?

import pandas as pd
d = pd.DataFrame({'a':[[1], [2], [3]], 'b': [[4], [5], [6]]})
d['combined'] = ''
d['combined'] = d.apply(lambda row: row.a + row.b, axis=1)
Phung Duy Phong
  • 876
  • 6
  • 18
2

You don't need to use apply for this:

d['c'] = d.a + d.b

    a    b    c
0  [1]  [4]  [1, 4]  
1  [2]  [5]  [2, 5]  
2  [3]  [6]  [3, 6]
Evan Nowak
  • 895
  • 4
  • 8
1

A simple d.sum(1) works

d['combined'] = d.sum(1)

    a   b   combined
0   [1] [4] [1, 4]
1   [2] [5] [2, 5]
2   [3] [6] [3, 6]
Vaishali
  • 37,545
  • 5
  • 58
  • 86