0

I have a table as below ( simplified one) enter image description here

I want to select each raw and compare its Col_b value with all other raw's Col_c value . If they are equal, assign/append the matching raw's Col_a value to selected raw's Col_d value

Resultant table will look like below enter image description here

The traditional method with multiple for/while loops I understand . Is there some efficient ways in pandas , numpy etc to solve this.

msraj
  • 37
  • 4

1 Answers1

2

You can use transform and np.where:

df = pd.DataFrame({'Col_a':['A1','Z7.6','B1.1','C2','A1.3','P7.6'],
                  'Col_b':[120,-1,78,180,6,8],
                  'Col_c':[-7,45,120,32,180,120]})
def f(x):
    rowindx = np.where(df.Col_c == x)
    res = ' '.join(df.loc[rowindx[0], 'Col_a'])
    return res

df['Col_d'] = df['Col_b'].transform(f)
df

Output:

  Col_a  Col_b  Col_c      Col_d
0    A1    120     -7  B1.1 P7.6
1  Z7.6     -1     45           
2  B1.1     78    120           
3    C2    180     32       A1.3
4  A1.3      6    180           
5  P7.6      8    120           

You can try this using apply and np.where:

def f(x):
    rowindx = np.where(df.Col_c == x.Col_b)
    res = ' '.join(df.loc[rowindx[0], 'Col_a'])
    return res

df['Col_d'] = df.apply(f, axis=1)
print(df)

Output:

  Col_a  Col_b  Col_c      Col_d
0    A1    120     -7  B1.1 P7.6
1  Z7.6     -1     45           
2  B1.1     78    120           
3    C2    180     32       A1.3
4  A1.3      6    180           
5  P7.6      8    120           
Scott Boston
  • 147,308
  • 15
  • 139
  • 187
  • Thanks , exactly the one I was looking . Found useful info on apply & transform form here https://stackoverflow.com/questions/27517425/apply-vs-transform-on-a-group-object – msraj Feb 04 '19 at 01:12