Skip to content
Advertisement

Change Series inplace in DataFrame after applying function on it

I’m trying to use pandas in order to change one of my columns in-place, using simple function.

After reading the whole Dataframe, I tried to apply function on one Series:

wanted_data.age.apply(lambda x: x+1)

And it’s working great. The only problem occurs when I try to put it back into my DataFrame:

wanted_data.age = wanted_data.age.apply(lambda x: x+1)

or:

wanted_data['age'] = wanted_data.age.apply(lambda x: x+1)

Throwing the following warning:

> C:Anacondalibsite-packagespandascoregeneric.py:1974:
> SettingWithCopyWarning: A value is trying to be set on a copy of a
> slice from a DataFrame. Try using .loc[row_indexer,col_indexer] =
> value instead
> 
> See the the caveats in the documentation:
> http://pandas.pydata.org/pandas-docs/stable
> /indexing.html#indexing-view-versus-copy   self[name] = value

Of Course, I can set the DataFrame using the long form of:

wanted_data.loc[:, 'age'] = wanted_data.age.apply(lambda x: x+1)

But is there no other, easier and more syntactic-nicer way to do it?

Thanks!

Advertisement

Answer

Use loc:

wanted_data.loc[:, 'age'] = wanted_data.age.apply(lambda x: x + 1)
Advertisement