0
votes

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

  a  b
0 0  0
1 0  1
2 1  0
3 1  1

I want a one-liner that without changing the original df would return this (let's say I'm applying xor function):

  a  b xor
0 0  0 0
1 0  1 1
2 1  0 1
3 1  1 0

What I'm doing now is (I work in the shell, so 2nd line prints the 'df'):

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

(foo gets a 'row' and let's say it does something with all the columns, so I'm not aiming to something column-specific, in the end I want to see all the original columns and the result column next to them).

I don't like this option because it's 2-step AND changing the original dataframe. My goal is to check the function again and again, so I want to be able to show the result next to the original values.

Is there an easy 1-step straightforward way of doing it?

Thanks,

2

2 Answers

2
votes

This should do the trick:

pd.concat([df,df.apply(foo,axis=1)],axis=1)
1
votes

This function is to show two data structures side by side. You need to create the second dataframe or series, or possibly use:

side_by_side(df1, df1.apply(foo))

Used in Wes McKinney's 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)

Example usage:

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