matthiash
matthiash

Reputation: 3233

Pandas: Appending a column to a data frame, based on the existing data, as a chained operation

I create a data frame:

import pandas as pd
df = pd.DataFrame({'FOO': [0,1,2], 'BAR': ['a','b','c']})

    FOO BAR
0   0   a
1   1   b
2   2   c

Now I filter the rows, and append a column, based on the values of an existing column:

df = df[lambda x: x['FOO']>0]
df['BAZ'] = df['BAR'].map(lambda x: x+'z')

    FOO BAR BAZ
1   1   b   bz
2   2   c   cz

Is it possible to do the filtering and appending of a column on one line, in an operation chaining manner? I'm able to do this:

df = df[lambda x: x['FOO']>0].join(df[lambda x: x['FOO']>0]['BAR'].map(lambda x: x+'z').rename('BAZ'))

But this is undesirable as I have to repeat the filtering.

Upvotes: 1

Views: 92

Answers (1)

ALollz
ALollz

Reputation: 59549

You can assign after the filter with a lambda:

df.loc[df.FOO.gt(0)].assign(BAZ = lambda x: x.BAR+'z')

#   FOO BAR BAZ
#1    1   b  bz
#2    2   c  cz

Also possible to assign first and then filter, but this wont be as performant if you filter many rows out:

df.assign(BAZ = df.BAR+'z').loc[df.FOO.gt(0)]

Upvotes: 1

Related Questions