16

I have follow simple DataFrame - df:

   0
0  1
1  2
2  3

Once I try to create a new columns and assign some values for them, as example below:

df['col2', 'col3'] = [(2,3), (2,3), (2,3)]

I got following structure

   0 (col2, col3)
0  1    (2, 3)
1  2    (2, 3)
2  3    (2, 3)

However, I am looking a way to get as here:

   0 col2, col3
0  1    2,   3
1  2    2,   3
2  3    2,   3
jpp
  • 159,742
  • 34
  • 281
  • 339
SpanishBoy
  • 2,105
  • 6
  • 28
  • 51
  • 1
    http://pandas.pydata.org/pandas-docs/stable/10min.html have you looked at the docs? It is pretty clear on how to create data frames. – Woody Pride Dec 03 '15 at 19:37
  • `df['col2', 'col3'] = [2,3]` would work fine. (in the case where all rows are identical) – smci Apr 19 '20 at 11:19

4 Answers4

23

Looks like solution is simple:

df['col2'], df['col3'] = zip(*[(2,3), (2,3), (2,3)])
SpanishBoy
  • 2,105
  • 6
  • 28
  • 51
  • 6
    While this code may answer the question, it would be better to include some context, explaining how it works and when to use it. Code-only answers are not useful in the long run. – Bono Dec 03 '15 at 20:30
  • 2
    Solution is simple if your problem is simple. Solution is useless if you want to assign 100 columns at the same time. – dreab Nov 06 '17 at 14:18
  • 2
    If you have optimized solution for assigning 100 columns feel free to share. – SpanishBoy Nov 07 '17 at 10:28
  • 1
    @SpanishBoy it's a bit frustrating that, so many years later, there's still no convenience syntax for this. The best you can do is a for loop: `for colname, data in zip(['col2', 'col3'], zip(*[(2, 3), (2, 3), (2, 3)])): df[colname] = data` – shadowtalker Apr 06 '18 at 14:11
  • 3
    @shadowtalker, I *think* there's a better way to do this.. you can assign a dataframe to `df[['col2', 'col3']]`, see my answer. – jpp Dec 17 '18 at 14:05
18

There is a convenient solution to joining multiple series to a dataframe via a list of tuples. You can construct a dataframe from your list of tuples before assignment:

df = pd.DataFrame({0: [1, 2, 3]})
df[['col2', 'col3']] = pd.DataFrame([(2,3), (2,3), (2,3)])

print(df)

   0  col2  col3
0  1     2     3
1  2     2     3
2  3     2     3

This is convenient, for example, when you wish to join an arbitrary number of series.

jpp
  • 159,742
  • 34
  • 281
  • 339
7

alternatively assign can be used

df.assign(col2 = 2, col3= 3)

https://pandas.pydata.org/pandas-docs/stable/reference/api/pandas.DataFrame.assign.html

as - if
  • 2,729
  • 1
  • 20
  • 26
3

I ran across this issue when trying to apply multiple scalar values to multiple new columns and couldn't find a better way. If I'm missing something blatantly obvious, let me know, but df[['b','c']] = 0 doesn't work. but here's the simplified code:

# Create the "current" dataframe
df = pd.DataFrame({'a':[1,2]})

# List of columns I want to add
col_list = ['b','c']

# Quickly create key : scalar value dictionary
scalar_dict = { c : 0 for c in col_list }

# Create the dataframe for those columns - key here is setting the index = df.index
df[col_list] = pd.DataFrame(scalar_dict, index = df.index)

Or, what appears to be slightly faster is to use .assign():

df = df.assign(**scalar_dict)
elPastor
  • 8,435
  • 11
  • 53
  • 81