learner
learner

Reputation: 877

In pandas dataframe, given two list columns, access the values of first column on the basis of the values in second column

I have Pandas data frame with two list columns:

   df = 
           a                          label
        [1, 2, 4, 5]                    [0, 0, 1, 0]
        [100, 12, 23, 4]                [1, 0, 0, 1]

Ultimately, I want to form a new column a_positive, which is also a list containing all the elements of a for which label is 1, so:

 df_output =
         a                                  label                a_positive            
        [1, 2, 4, 5]                    [0, 0, 1, 0]              [4]
        [100, 12, 23, 4]                [1, 0, 0, 1]              [100, 4]

Is it possible to do so using apply and zip ing the lists together??

Upvotes: 1

Views: 43

Answers (4)

Ahx
Ahx

Reputation: 7995

Try assign

print(df.assign(a_positive=lambda x: [[df['a'][key][idx] for idx in [c for c, val in enumerate(array) if val == 1]] for key, array in enumerate(df['label'])]))

Output:

                  a         label a_positive
0      [1, 2, 4, 5]  [0, 0, 1, 0]        [4]
1  [100, 12, 23, 4]  [1, 0, 0, 1]   [100, 4]

Upvotes: 1

deadshot
deadshot

Reputation: 9071

Try this

import numpy as np

df['a'] = df['a'].apply(np.array)
df['a_positive'] = df.apply(lambda x: x.a[list(map(bool, x.label))],axis=1)
print(df)

Output:

                  a         label a_positive
0      [1, 2, 4, 5]  [0, 0, 1, 0]        [4]
1  [100, 12, 23, 4]  [1, 0, 0, 1]   [100, 4]

Upvotes: 0

Andy L.
Andy L.

Reputation: 25259

Try list comprehension

df['a_positive'] = [[a[i] for i,x in enumerate(b) if x] for a, b in zip(df.a, df.label)]

Out[440]:
                  a         label a_positive
0      [1, 2, 4, 5]  [0, 0, 1, 0]        [4]
1  [100, 12, 23, 4]  [1, 0, 0, 1]   [100, 4]

Upvotes: 2

BENY
BENY

Reputation: 323326

Try with

df['new']=df.apply( lambda x : [s  for s, t in zip(x['a'],x['label']) if t==1],axis=1)
df
Out[78]: 
                  a         label       new
0      [1, 2, 4, 5]  [0, 0, 1, 0]       [4]
1  [100, 12, 23, 4]  [1, 0, 0, 1]  [100, 4]

Upvotes: 2

Related Questions