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.

7
  • 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. Commented Jan 29, 2019 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 explains why this syntax doesn't work. Commented Jan 29, 2019 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. Commented Jan 29, 2019 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. Commented Jan 29, 2019 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. Commented Jan 29, 2019 at 22:37

2 Answers 2

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]))

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

Comments

0

I think you want to do

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

1 Comment

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.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.