Pandas - show application results next to the source data framework

I have a pandas DataFrame, then I apply the function to a column of columns and I get a new column of results. Then I want to check the results of a new column with the original column values.

ab 0 0 0 1 0 1 2 1 0 3 1 1 

I want a single-line, without changing the original df, to return this (suppose I use the xor function):

  ab xor 0 0 0 0 1 0 1 1 2 1 0 1 3 1 1 0 

What am I doing now (I work in a shell, so the second line prints "df"):

 df['result'] = df.apply(xor, axis=1) df 

(foo gets the "row", and let it do something with all the columns, so I am not aiming for something specific to the column, in the end I want to see all the original columns and the result column next to them).

I do not like this parameter because it is two-stage AND changing the original frame. My goal is to test the function again and again, so I want to show the result next to the original values.

Is there a simple 1-step easy way to do this?

Thanks,

0
source share
2 answers

This should do the trick:

 pd.concat([df,df.apply(foo,axis=1)],axis=1) 
+2
source

This function should show two data structures side by side. You need to create a second data block or series, or perhaps use:

 side_by_side(df1, df1.apply(foo)) 

Used in Wes McKinney presentation :

 def side_by_side(*objs, **kwds): from pandas.core.common import adjoin space = kwds.get('space', 4) reprs = [repr(obj).split('\n') for obj in objs] print adjoin(space, *reprs) 

Usage example:

 df1 = pd.DataFrame(np.random.rand(10,3)) df2 = pd.DataFrame(np.random.rand(10,3)) side_by_side(df1, df2) 0 1 2 0 1 2 0 0.424040 0.264855 0.025566 0 0.708604 0.306784 0.645058 1 0.932507 0.522241 0.915979 1 0.839926 0.911550 0.152661 2 0.291583 0.318066 0.242179 2 0.085973 0.522005 0.960857 3 0.953090 0.126239 0.161788 3 0.481225 0.093825 0.191482 4 0.739598 0.834832 0.583147 4 0.053519 0.954738 0.846373 5 0.633130 0.740508 0.335575 5 0.281024 0.244012 0.661398 6 0.158822 0.399715 0.890146 6 0.842356 0.090806 0.562401 7 0.864191 0.917889 0.715713 7 0.011725 0.274691 0.729003 8 0.139880 0.662035 0.943423 8 0.105924 0.405305 0.140807 9 0.544886 0.577185 0.623016 9 0.192138 0.558908 0.633519 
+1
source

All Articles