1

I recently answered a question where the OP was looking multiple columns with multiple different values to an existing dataframe (link). And it's fairly succinct, but I don't think very fast.

Ultimately I was hoping I could do something like:

# Existing dataframe
df = pd.DataFrame({'a':[1,2]})

df[['b','c']] = 0

Which would result in:

a   b   c
1   0   0
2   0   0

But it throws an error.

Is there a super simple way to do this that I'm missing? Or is the answer I posted earlier the fastest / easiest way?

NOTE

I understand this could be done via loops, or via assigning scalars to multiple columns, but am trying to avoid that if possible. Assume 50 columns or whatever number you wouldn't want to write:

df['b'], df['c'], ..., df['xyz'] = 0, 0, ..., 0

Not a duplicate:

The "Possible duplicate" question suggested to this shows multiple different values assigned to each column. I'm simply asking if there is a very easy way to assign a single scalar value to multiple new columns. The answer could correctly and very simply be, "No" - but worth knowing so I can stop searching.

elPastor
  • 8,435
  • 11
  • 53
  • 81
  • 1
    Would you be happy with `df.assign(**{k:0 for k in ["b", "c"]})`? **Edit**: I see that you have that in your linked post. AFAIK, I don't think there are any other ways than what's in the post I linked but we shall see if any of the pandas experts have an idea. – pault Jan 29 '19 at 22:07
  • I still maintain that this is a dupe. The fact that it's a single scalar value hardly makes a difference (IMO). The [answer here](https://stackoverflow.com/a/39050916/5858851) explains why this syntax doesn't work. – pault Jan 29 '19 at 22:11
  • I'll give you that - the explanation certainly does provide a lot of insight as to why my syntax doesn't work, but the *question* is different as the intent is to apply a single scalar value to multiple new columns. Like you said, I'd like to see if anyone has some stroke of genius before completely killing this post. – elPastor Jan 29 '19 at 22:15
  • 1
    Slight variation which works well for scalars: `df.assign(**dict.fromkeys(['b', 'c'], 0))`. But, again, not sure if this is what you want. – jpp Jan 29 '19 at 22:29
  • @jpp - that might be the winner right there if there's no simpler way. Wonder if Wes et. al have thought through this and decided it wasn't necessary to allow a scalar to be applied to a dataframe. – elPastor Jan 29 '19 at 22:37
  • I would guess so, assigning one isn’t bad and can be useful but why replicate 50 scalars by the length of the DataFrame, especially when you could be working with millions of rows. I’d think it’s preferable to create a new dataframe or simply store the constants in a dictionary or class at that point. – ALollz Jan 29 '19 at 22:43
  • Right, for the most efficient use of resources, a separate dictionary is probably better for a lot of reasons - except code comprehension. But I get your point, and worth more thought. Thanks all. Mark as dupe if you feel strongly. – elPastor Jan 29 '19 at 22:47

2 Answers2

1

Why not using assign

df.assign(**dict.fromkeys(['b','c'],0))
Out[781]: 
   a  b  c
0  1  0  0
1  2  0  0

Or create the dict by d=dict(zip([namelist],[valuelist]))

BENY
  • 317,841
  • 20
  • 164
  • 234
0

I think you want to do

df['b'], df['c'] = 0, 0
Samuel Nde
  • 2,565
  • 2
  • 23
  • 23
  • Nde - thank you for your answer, but I'm looking to assign a scalar value to a dataframe as opposed to multiple series. I've updated my original question as such. – elPastor Jan 29 '19 at 22:23