Skip to content Skip to sidebar Skip to footer

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 Serie: wanted_dat

Solution 1:

Use loc:

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

Solution 2:

I would suggest wanted_data['age']= wanted_data['age'].apply(lambda x: x+1),then save file as wanted_data.to_csv(fname,index=False), where "fname" is the name of a file to be updated.

Solution 3:

I cannot comment, so I'll leave this as an answer.

Because of the way chained indexing is hundled internally, you may get back a deep copy, instead of a reference to your initial DataFrame (For more see chained assignment - this is a very good source. Bare .loc[] always returns a reference). Thus, you may not assign back to your DataFrame, but to a copy of it. On the other hand, your format may return a reference to your initial Dataframe and, while mutating it, the initial DataFrame will mutate, too. Python prints this warning to beat the drum for the situation, so as the user can decide whether this is the wanted treatment or not.

If you know what you're doing, you can silence the warning using:

with pd.options.mode.chained_assignment = "None":
    wanted_data.age = wanted_data.age.apply(lambda x: x+1)

If you think that this is an important manner (e.g. there is the possibility of unintentionally mutating the initial DataFrame), you can set the above option to "raise", so that an error would be raised, instead of a warning.

Also, I think usage of the term "inplace" is not fully correct. "inplace" is used as an argument at some methods, so as to mutate an object without assigning it to itself (the assignment is hundled internally), and apply() does not support this feature.

Post a Comment for "Change Series Inplace In Dataframe After Applying Function On It"