4

Here is my dataframe:

| col1     | col2      | col3     |
----------------------------------
[1,2,3,4] | [1,2,3,4] | [1,2,3,4]

I also have this function:

def joiner(col1,col2,col3):
    snip = []
    snip.append(col1)
    snip.append(col2)
    snip.append(col3)
    return snip

I want to call this on each of the columns and assign it to a new column.

My end goal would be something like this:

| col1     | col2      | col3     | col4
------------------------------------------------------------------
[1,2,3,4] | [1,2,3,4] | [1,2,3,4] | [[1,2,3,4],[1,2,3,4],[1,2,3,4]]
1

2 Answers 2

4

Just .apply list on axis=1, it'll create lists for each rows

>>> df['col4'] = df.apply(list, axis=1)

OUTPUT:

           col1          col2          col3  col4
0  [1, 2, 3, 4]  [1, 2, 3, 4]  [1, 2, 3, 4]  [[1, 2, 3, 4], [1, 2, 3, 4], [1, 2, 3, 4]]
Sign up to request clarification or add additional context in comments.

Comments

0

You can just do

df['col'] = df.values.tolist()

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.