Why use pandas.assign rather than simply initialize new column?

后端 未结 2 1584
余生分开走
余生分开走 2020-12-30 00:13

I just discovered the assign method for pandas dataframes, and it looks nice and very similar to dplyr\'s mutate in R. However, I\'ve always gotten

2条回答
  •  感动是毒
    2020-12-30 00:54

    The premise on assign is that it returns:

    A new DataFrame with the new columns in addition to all the existing columns.

    And also you cannot do anything in-place to change the original dataframe.

    The callable must not change input DataFrame (though pandas doesn't check it).

    On the other hand df['ln_A'] = np.log(df['A']) will do things inplace.


    So is there a reason I should stop using my old method in favour of df.assign?

    I think you can try df.assign but if you do memory intensive stuff, better to work what you did before or operations with inplace=True.

提交回复
热议问题