3

New to Python and this is the latest challenge I need help with

in df1 I have the following spread of data:

data = {'Code': ['53.110.30.0140.','53.110.30.0200.','53.110.30.0240. ','53.110.3130.','53.110.3140.'],
       'AA': ['53','53','53','53','53'],
       'BBB': ['110','110','110','110','110'],
       'CC': ['30','30','30','31','31'],
      'DDDD': ['0140','0200','0240','0030','0040']}

df1 = pd.DataFrame(data, columns=['Code','AA','BBB','CC','DDDD'])

in df2 I have the following spread of data:

data2 = {'Size': ['0005','0007','0010','0015','0020','0030','0040','0060','0080','0100','0120','0140','0160','0180','0200','0220','0240'],
         '10': [3,5,2,35,47,19,5,14,27,25,46,18,46,38,17,17,23],
         '31': [48,11,26,4,28,25, 19,42,33,41,25,37,35,5,2,27,29],
         '41': [1,32,38,12,15,39,6,46,5,36,35,25,13,12,50,19,19],
         '20': [6,10,7,4,47,25,20,44,20,31,9,44,26,17,20,33,39],
         '21': [21,31,44,12,18,40,8,4,18,32,34,40,29,10,6,14,22],
         '30': [34,50,3,32,8,31,35,40,30,16,37,11,39,16,12,36,22],
         '32': [41,25,32,11,35,18,49,12,31,4,24,8,14,29,28,12,7],
         '40': [48,42,9,11,8,10,39,50,40,46,10,9,42,33,24,7,48],
         '42': [20,21,21,3,23,50,8,41,7,14,14,5,19,9,13,41,48],
         '50': [11,22,24,15,29,46,10,39,46,38,14,13,24,8,14,22,35],
         '51': [41,47,18,26,34,4,19,7,36,13,27,20,21,49,46,30,16],
         '60': [1,1,13,12,1,18,26,7,21,45,5,18,13,4,50,16,15],
         '61': [10,18,24,12,25,48,19,32,5,40,37,8,32,14,15,1,20],
         '62': [34,34,8,21,8,25,7,10,38,16,32,49,21,31,28,13,41]}

df2 = pd.DataFrame(data2, columns=['Size','10','31','41','20','21','30','32','40','42','50','51','60','61','62'])

What I'm trying to achieve is having a column added to df1 called 'values' with a value from df2 where the two below matching criteria are met (in other words where they intersect, that's the value I need)

  • df1['DDDD'] values matches in df2['SIZE']
  • df1['CC'] values matches to columns [10][31][41][20][21][30][32][40][42][50][51][60][61][62]

As mentioned, the desired result would be returning the intersecting value, for example, if df1['DDDD'] = '0140' and df1['CC'] = '30', the value returned from df2 should equal '11'.

I've tried using merge and map but have come up against a wall as my column names do not match

Hope I've been clear with explaining this.

Thanks in advance!

MJKing
  • 57
  • 3
  • kindly post your expected output – sammywemmy Dec 04 '20 at 21:53
  • Hi thank you for responding so quickly - not sure how to post a picture just yet but the code would be: data = {'Code': ['53.110.30.0140.','53.110.30.0200.','53.110.30.0240. ','53.110.3130.','53.110.3140.'], 'AA': ['53','53','53','53','53'], 'BBB': ['110','110','110','110','110'], 'CC': ['30','30','30','31','31'], 'DDDD': ['0140','0200','0240','0030','0040'], 'value' : ['11','12','22','25','19']} df1 = pd.DataFrame(data, columns=['Code','AA','BBB','CC','DDDD','value']) – MJKing Dec 04 '20 at 22:01
  • @QuaangHoang's answer below solves it well. I think you should pick it as the answer – sammywemmy Dec 04 '20 at 22:07
  • Agreed! Thanks for responding so quickly I'm very happy with what I have learned – MJKing Dec 04 '20 at 22:31

1 Answers1

2

Looks like a lookup job:

df2.set_index('Size', inplace=True)
df1['Out'] = df2.lookup(df1['DDDD'], df1['CC'])

Output:

               Code  AA  BBB  CC  DDDD  Out
0   53.110.30.0140.  53  110  30  0140   11
1   53.110.30.0200.  53  110  30  0200   12
2  53.110.30.0240.   53  110  30  0240   22
3      53.110.3130.  53  110  31  0030   25
4      53.110.3140.  53  110  31  0040   19
Quang Hoang
  • 146,074
  • 10
  • 56
  • 74