Reputation: 531
I'm learning data exploration in Python. While practising the pandas
library, I saw two functions named df.assign()
and df.apply()
. The definition of both functions looked very similar. Can you please explain to me these two functions and their unique use cases?
Upvotes: 9
Views: 9343
Reputation: 878
assign() method assign new columns to a DataFrame, returning a new object (a copy) with the new columns added to the original ones. Existing columns that are re-assigned will be overwritten.
Apply a function along an axis of the DataFrame. apply() allow the users to pass a function and apply it on every single value of the Pandas series.
Upvotes: 1
Reputation: 352
The difference concerns whether you wish to modify an existing frame, or create a new frame while maintaining the original frame as it was.
In particular, DataFrame.assign
returns you a new object that has a copy of the original data with the requested changes, the original frame remains unchanged.
For example:
df = DataFrame({'A': range(1, 11), 'B': np.random.randn(10)})
If you wish to create a new frame in which A
is everywhere 1
without destroying df
, you could use .assign
new_df = df.assign(A=1)
Although .apply
is not intended to be used to modify a dataframe, there is no guarantee that applying the function will not change the dataframe.
Upvotes: 11