Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Creating a new column from two columns with apply()

I want to creat a column s['C'] using apply() with a Pandas DataFrame.

My dataset is similiar to this:

[In]:

s=pd.DataFrame({'A':['hello', 'good', 'my', 'pandas','wrong'], 
                'B':[['all', 'say', 'hello'],
                     ['good', 'for', 'you'], 
                     ['so','hard'], 
                     ['pandas'],
                     []]})
[Out]: 
    A       B
0   hello   [all, say, hello]
1   good    [good, for, you]
2   my      [so, hard]
3   pandas  [pandas]
4   wrong   []

I need to creat a s['C'] column where the value of each row is a list with ones and zeros dependending if the word of column A is in the list of column B and the position of the element in the list of column B. My output should be like this:

[Out]: 
    A       B                   C
0   hello   [all, say, hello]   [0, 0, 1]
1   good    [good, for, you]    [1, 0, 0]
2   my      [so, hard]          [0, 0]
3   pandas  [pandas]            [1]
4   wrong   []                  [0]

I've been trying with a función and apply, but I still have not realized where is the error.

[In]:
def func(valueA,listB):
  new_list=[]
  for i in listB:
    if listB[i] == valueA:
      new_list.append(1)
    else:
      new_list.append(0)
  return new_list

s['C']=s.apply( lambda x: func(x.loc[:,'A'], x.loc[:,'B']))

The error is: Too many indexers

And I also tried with:

[In]:
list=[]
listC=[]
for i in s['A']:
  for j in s['B'][i]:
     if s['A'][i] == s['B'][i][j]:
        list.append(1)
     else:
        list.append(0)
  listC.append(list)

s['C']=listC

The error is: KeyError: 'hello'

Any suggests?

like image 773
Nachengue Avatar asked May 11 '20 15:05

Nachengue


1 Answers

If you are working with pandas 0.25+, explode is an option:

(s.explode('B')
  .assign(C=lambda x: x['A'].eq(x['B']).astype(int))
  .groupby(level=0).agg({'A':'first','B':list,'C':list})
)

Output:

        A                  B          C
0   hello  [all, say, hello]  [0, 0, 1]
1    good   [good, for, you]  [1, 0, 0]
2      my         [so, hard]     [0, 0]
3  pandas           [pandas]        [1]
4   wrong              [nan]        [0]

Option 2: Based on your logic, you can do a list comprehension. This should work with any version of pandas:

s['C'] = [[x==a for x in b] if b else [0] for a,b in zip(s['A'],s['B'])]

Output:

        A                  B                     C
0   hello  [all, say, hello]  [False, False, True]
1    good   [good, for, you]  [True, False, False]
2      my         [so, hard]        [False, False]
3  pandas           [pandas]                [True]
4   wrong                 []                   [0]
like image 150
Quang Hoang Avatar answered Sep 20 '22 15:09

Quang Hoang