See SO question: http://stackoverflow.com/questions/19802708/add-column-to-pandas-dataframe-not-in-place

I would suggest something like:

df_new  = df.alter(new_col_name=existing_series)

where alter would wake one or more columns:

df_new  = df.alter(
new_col_name1=existing_series1,
new_col_name2=existing_series2,
)

This can further be a superset of drop allowing columns to be removed by setting them to None

df_new  = df.alter(old_col_name=None)

Thoughts?

Comment From: jreback

better to just add an inplace arg to insert (and default it to True); and allow -1 for loc to mean last

e.g.

df.insert(-1,'A',series,inplace=False)

Comment From: cancan101

@jreback

That would work, but would still be clunky if there are a few columns I want to add and/or delete.

Comment From: jreback

why would you not just copy and add? or just construct a new one and concat?

Comment From: cancan101

Under the hood, that is essentially what would be happening. IMHO, the proposed alter method would just be less typing and more readable.

Comment From: cancan101

You could even extend alter to support renaming: either:

df_new = df.alter(old_name=new_name)

or:

df_new = df.alter(new_name=old_name)

Not sure which ordering make more sense.

Comment From: jreback

this is very confusing, you want alter to return a new frame?

Comment From: cancan101

Yes. Maybe alter is not the best name :-)

Comment From: ghost

This seems like a personal coding style preference to me, addressable by other means. Remember, you can always use monkey patching to add convenience methods to the class.

-1 from me.

Comment From: jtratner

I'm also -1 on this.

This would be less explicit and less useful than the existing methods. It's nearly the same as concat, but without alignment, join and merge could also be used, or use assignment in a for loop.

You can also define a single function that takes a dataframe, column name and a value (call it setitem) that does what you want.

If you want to change column names, you can set the columns property directly.

How is this different from the above?

Under the hood, that is essentially what would be happening. IMHO, the proposed alter method would just be a less typing and more readable.

— Reply to this email directly or view it on GitHubhttps://github.com/pydata/pandas/issues/5451#issuecomment-27878375 .

Comment From: cancan101

Agreed, there are alternatives that are functionally equivalent. Is there any value in a solution that is "less typing and more readable"?

Comment From: jtratner

What I find surprising is that DataFrame has an update method, but it doesn't accept new columns. If you think of DataFrame like a dict, with a dict you can pass (key, value) pairs to update to add an iterable to the dictionary. Seems like if you passed new columns to update, it should add them to the DataFrame (because they are all nan) and existing columns would have the existing NA-filling behavior.

Comment From: jtratner

df.update(dict(new_col: new_ser, other_new_col: other_new_ser)) could make sense and echos the behavior of behaves-like-a-dict.

Comment From: jtratner

but that would be inplace.

Comment From: jreback

prob is that defeats the ability of update to align (as it would be forced to update/add everything)

Comment From: cancan101

What do you think about @jreback's suggestion here: https://github.com/pydata/pandas/issues/5451#issuecomment-27877586

Comment From: jtratner

Maybe extending append would make more sense?

Comment From: jorisvandenbossche

The added DataFrame.assign a while ago closes this issue I think