Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

I have two columns with list in in. How do I combine these two list into a new column element for element

So I have for example a DataFrame with the two columns:

col1               col2
['1', '2', '3']    ['A', 'B', 'C']
['4', '5', '6']    ['D', 'E', 'F']
etc.

I would like to get a third column with:

col3
[['1', 'A'], ['2', 'C'], ['3', 'C']]
[['4', 'D'], ['5', 'E'], ['6', 'F']] 
etc

I have tried to use apply and combine it with a lambda function like this:

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

df['col3'] = df.apply(lambda x: [y,z] for y,z in zip(x['col1'], x['col2']), axis=1)

But this only give the error:

SyntaxError: Generator expression must be parenthesized

Can some help me?

>Solution :

In your solution add [] for list comprehension:

df['col3'] = df.apply(lambda x: [[y,z] for y,z in zip(x['col1'], x['col2'])], axis=1)
print (df)
        col1       col2                      col3
0  [1, 2, 3]  [A, B, C]  [[1, A], [2, B], [3, C]]
1  [4, 5, 6]  [D, E, F]  [[4, D], [5, E], [6, F]]

Or use nested list comprehension:

df['col3'] = [[[a, b] for a, b in zip(*x)] for x in zip(df['col1'], df['col2'])]
print (df)
        col1       col2                      col3
0  [1, 2, 3]  [A, B, C]  [[1, A], [2, B], [3, C]]
1  [4, 5, 6]  [D, E, F]  [[4, D], [5, E], [6, F]]
Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading