3

In a pandas DataFrame, foo for example:

>>> foo
   col1  col2
0     1     0
1     2     0
2     3     1

If you want to add a new column, all with the same value you can do

>>> foo['col3'] = 1
>>> foo
   col1  col2  col3
0     1     0     1
1     2     0     1
2     3     1     1

If you want to add another new column, all with specific values you can do

>>> foo['col4'] = ['a', 'b', 'c']
>>> foo
   col1  col2  col3  col4
0     1     0     1     a
1     2     0     1     b
2     3     1     1     c

But what I want to do is add the same list to each row as new column. Something like

>>> myList = [True, False, False, True]
>>> foo['col5'] = {...something something something...}
>>> foo
   col1  col2  col3  col4                        col5
0     1     0     1     a  [True, False, False, True]
1     2     0     1     b  [True, False, False, True]
2     3     1     1     c  [True, False, False, True]

Using the previous method results in ValueError('Length of values does not match length of ' 'index'). So at the moment, my {...something something something...} line is foo['col5'] = [myList] * foo.shape[0]. But I'm wondering, is there a better way?

5
  • 1
    Do you really want references to the same list in each row? Because that's what your current solution does. Commented Apr 9, 2018 at 3:46
  • You can try foo['col5'] = [myList]*len(foo) Commented Apr 9, 2018 at 3:57
  • @0p3n5ourcE Notice that suffers from a certain issue... Commented Apr 9, 2018 at 3:58
  • @cᴏʟᴅsᴘᴇᴇᴅ Not sure, what did I miss, is it performance or causes error? Commented Apr 9, 2018 at 4:01
  • 1
    @0p3n5ourcE Edited my answer :) Commented Apr 9, 2018 at 4:02

1 Answer 1

8

Use a list comprehension.

v = [True, False, False, True]
df['col5'] = [v for _ in range(len(df))]

df
   col1  col2                        col5
0     1     0  [True, False, False, True]
1     2     0  [True, False, False, True]
2     3     1  [True, False, False, True]

You might be tempted to use

df['col5'] = [True, False, False, True] * len(df)

However, each record actually references the same list. Try this -

df.loc[0, 'col5'][0] = False
df

   col1  col2                         col5
0     1     0  [False, False, False, True]
1     2     0  [False, False, False, True]
2     3     1  [False, False, False, True]

You'll see the change is reflected across all sublists.

Sign up to request clarification or add additional context in comments.

1 Comment

Great! I forgot about it :) +1

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.