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 Serie:
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:\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 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!
No, the apply() method doesn't contain an inplace parameter, unlike these pandas methods which have an inplace parameter: df. drop()
Change data type of a series in PandasUse a numpy. dtype or Python type to cast entire pandas object to the same type. Alternatively, use {col: dtype, …}, where col is a column label and dtype is a numpy. dtype or Python type to cast one or more of the DataFrame's columns to column-specific types.
You can replace values of all or selected columns based on the condition of pandas DataFrame by using DataFrame. loc[ ] property. The loc[] is used to access a group of rows and columns by label(s) or a boolean array. It can access and can also manipulate the values of pandas DataFrame.
Use loc
:
wanted_data.loc[:, 'age'] = wanted_data.age.apply(lambda x: x + 1)
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With