Change series in place in DataFrame after applying function on it

I am trying to use pandasto change one of my columns in place using a simple function.

After reading the entire Dataframe, I tried to apply the function in one series:

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

And it works great. The only problem occurs when I try to return it to 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:\Anaconda\lib\site-packages\pandas\core\generic.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 a long form:

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

But is there no other, simpler and more syntactic way to do this?

Thank!

+4
source share
2 answers

Use loc:

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

wanted_data['age']= wanted_data['age'].apply(lambda x: x+1), wanted_data.to_csv(fname,index=False) "fname" - .

0

All Articles