-1

A, B, C and D are input columns,

Input:

 A   B    C    D  
SG   TC   Y1  8  
SG   TC   Y2  2  
SG   TC   Y3  12 
SG1  TC   Y1  12 
SG1  TC   Y2  2 
SG1  TC   Y3  12 
SG1  TC1  Y1  12 
SG1  TC1  Y2  10 
SG1  TC1  Y3  9 

Require output

A   B   C   D  Out put
SG  TC  Y1  8  Y1+Y2
SG  TC  Y2  2  Y1+Y2
SG  TC  Y3  12 Y3
SG1 TC  Y1  12 Y1+Y2
SG1 TC  Y2  2  Y1+Y2
SG1 TC  Y3  12 Y3
SG1 TC1 Y1  12 Y1
SG1 TC1 Y2  10 Y1+Y3
SG1 TC1 Y3  9  Y1+Y3
Sociopath
  • 13,068
  • 19
  • 47
  • 75
  • 2
    Please elaborate the logic how you got that output and what have you tried so far? – Sociopath Jun 28 '19 at 10:38
  • if col-D value is less than 10 than merge Y1 with Y2, Y2 with Y1 and Y3 with Y2 but within same cluster My cluster are (First cluster is SG TC, Second Cluster SG1,TC and Third Cluster is SG1,TC1) – Kish_Raj05 Jun 28 '19 at 10:55
  • 1
    Are you asking how to write the logic statements for your particular case or how to add a new column to a pandas DataFrame? – Novice Jun 28 '19 at 11:30

1 Answers1

0

This answer covers how to add columns to pandas DataFrames based on logical statements. You will need the more complicated np.select option.

I don't fully understand what logic you want but I'm guessing your conditions would look something like the following

conditions = [
    (df['D'] < 10) & (df['B'] == 'TC') & (df['C'] == 'Y1'),
    (df['D'] < 10) & (df['B'] == 'TC') & (df['C'] == 'Y3'),
    ...,
    (df['A'] == 'SG1') & (df['B'] == 'TC') & (df['D'] <= 10)]
choices = [Y1+Y2, Y3,..., Y1+Y3]

It seems like you have a lot of conditions, so fill in all your additional cases where I left ellipses.

Novice
  • 855
  • 8
  • 17