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
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!
source
share