Pandas column card in place

I spent some time searching on Google and did not find the answer to a simple question: how can I map the Pandas dataframe column in place? Let's say I have the following df:

In [67]: frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon']) In [68]: frame Out[68]: bde Utah -1.240032 1.586191 -1.272617 Ohio -0.161516 -2.169133 0.223268 Texas -1.921675 0.246167 -0.744242 Oregon 0.371843 2.346133 2.083234 

And I want to add 1 to each value of column b . I know I can do this:

 In [69]: frame['b'] = frame['b'].map(lambda x: x + 1) 

Or how is it - AFAIK there is no difference between map and apply in the context of Series (except that map can also accept dict or Series ) - correct me if I'm Wrong:

 In [71]: frame['b'] = frame['b'].apply(lambda x: x + 1) 

But I don't like to specify 'b' twice. Instead, I would like to do something like this:

 frame['b'].map(lambda x: x + 1, inplace=True) 

Is it possible?

+5
source share
2 answers
 frame Out[6]: bde Utah -0.764764 0.663018 -1.806592 Ohio 0.082226 -0.164653 -0.744252 Texas 0.763119 1.492637 -1.434447 Oregon -0.485245 -0.806335 -0.008397 frame['b'] +=1 frame Out[8]: bde Utah 0.235236 0.663018 -1.806592 Ohio 1.082226 -0.164653 -0.744252 Texas 1.763119 1.492637 -1.434447 Oregon 0.514755 -0.806335 -0.008397 

Edit to add:

If this is an arbial function and you really need to apply for a place, you can write a thin wrapper around pandas to handle it. Personally, I canโ€™t imagine a time when it would be critical that you donโ€™t have to use the standard implementation (if, perhaps, you do not write tons of code and cannot worry about writing additional directors, maybe?)

 from pandas import DataFrame import numpy as np class MyWrapper(DataFrame): def __init__(self, *args, **kwargs): super(MyWrapper,self).__init__(*args,**kwargs) def myapply(self,label, func): self[label]= super(MyWrapper,self).__getitem__(label).apply(func) df = frame = MyWrapper(np.random.randn(4, 3), columns=list('bde'), index=['Utah', 'Ohio', 'Texas', 'Oregon']) print df df.myapply('b', lambda x: x+1) print df 

It gives:

 >> bde Utah -0.260549 -0.981025 1.136154 Ohio 0.073732 -0.895937 -0.025134 Texas 0.555507 -1.173679 0.946342 Oregon 1.871728 -0.850992 1.135784 bde Utah 0.739451 -0.981025 1.136154 Ohio 1.073732 -0.895937 -0.025134 Texas 1.555507 -1.173679 0.946342 Oregon 2.871728 -0.850992 1.135784 

Obviously this is a very minimal example, hopefully that provides some methods that are interesting to you.

+6
source

You can use add

 In [2]: import pandas as pd In [3]: import numpy as np In [4]: frame = pd.DataFrame(np.random.randn(4, 3), columns=list('bde'), index= ...: ['Utah', 'Ohio', 'Texas', 'Oregon']) In [5]: frame.head() Out[5]: bde Utah -1.165332 -0.999244 -0.541742 Ohio -0.319887 0.199094 -0.438669 Texas -1.242524 -0.385092 -0.389616 Oregon 0.331593 0.505496 1.688962 In [6]: frame.b.add(1) Out[6]: Utah -0.165332 Ohio 0.680113 Texas -0.242524 Oregon 1.331593 Name: b, dtype: float64 In [7]: 
0
source

Source: https://habr.com/ru/post/1245137/


All Articles