Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Change Series inplace in DataFrame after applying function on it

Tags:

python

pandas

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!

like image 290
Yam Mesicka Avatar asked May 16 '15 14:05

Yam Mesicka


People also ask

Is apply function inplace?

No, the apply() method doesn't contain an inplace parameter, unlike these pandas methods which have an inplace parameter: df. drop()

How do I change my series on pandas?

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.

How do I change the values in pandas series based on conditions?

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.


1 Answers

Use loc:

wanted_data.loc[:, 'age'] = wanted_data.age.apply(lambda x: x + 1) 
like image 95
Alexander Avatar answered Oct 14 '22 17:10

Alexander