user3230153 user3230153 - 1 year ago 552
Python Question

Combining two columns give ValueError: Wrong number of items passed 2, placement implies 1

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.

Answer Source

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)
Recommended from our users: Dynamic Network Monitoring from WhatsUp Gold from IPSwitch. Free Download