Pandas: use map function to LOOKUP a value in another df










0















I'd like to use the map function to update values in df1 based on the looked up value in df2The lookup column is ISIN_CUSIP_CODE



df1 = [('ISIN_CUSIP_CODE', ['US68323ABL70', '9128284D9', '912828W89', 'CA135087J470','CA135087J470','912796QP7','US20030NCM11','US912810SD19','XS1851277969',]),
('Product', ['GOVT', 'GOVT', 'GOVT', 'GOVT', 'GOVT', 'GOVT', '', '', '',]),
]
df1 = pd.DataFrame.from_items(df1)
print(df1)
df2 = [('ISIN_CUSIP_CODE', ['US20030NCM11', 'US912810SD19', 'XS1851277969', 'XS1391086987', 'CA064151BL66', 'CA13595ZZ661', ]),
('Product_MRD', ['CORP', 'GOVT', 'CORP', 'CORP','CORP','CORP',]),
]
df2 = pd.DataFrame.from_items(df2)
print(df2)

df1
ISIN_CUSIP_CODE Product
0 US68323ABL70 GOVT
1 9128284D9 GOVT
2 912828W89 GOVT
3 CA135087J470 GOVT
4 CA135087J470 GOVT
5 912796QP7 GOVT
6 US20030NCM11
7 US912810SD19
8 XS1851277969
df2
ISIN_CUSIP_CODE Product_MRD
0 US20030NCM11 CORP
1 US912810SD19 GOVT
2 XS1851277969 CORP
3 XS1391086987 CORP
4 CA064151BL66 CORP
5 CA13595ZZ661 CORP


My map function is not returning the looked up values in df2



df1['Product'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])
print(df1)

ISIN_CUSIP_CODE Product
0 US68323ABL70 GOVT
1 9128284D9 GOVT
2 912828W89 GOVT
3 CA135087J470 GOVT
4 CA135087J470 GOVT
5 912796QP7 GOVT
6 US20030NCM11
7 US912810SD19
8 XS1851277969









share|improve this question






















  • Typo: use df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])

    – Sandeep Kadapa
    Nov 16 '18 at 7:52











  • Use np.where(df1['Product'].replace('',np.nan).isnull(),df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD']),df1['Product'])

    – Sandeep Kadapa
    Nov 16 '18 at 7:55











  • Getting NameError: name 'np' is not defined. Does np need to be prefixed/

    – Peter Lucas
    Nov 16 '18 at 7:57











  • import numpy as np

    – Sandeep Kadapa
    Nov 16 '18 at 7:58











  • Sorry same result

    – Peter Lucas
    Nov 16 '18 at 8:01















0















I'd like to use the map function to update values in df1 based on the looked up value in df2The lookup column is ISIN_CUSIP_CODE



df1 = [('ISIN_CUSIP_CODE', ['US68323ABL70', '9128284D9', '912828W89', 'CA135087J470','CA135087J470','912796QP7','US20030NCM11','US912810SD19','XS1851277969',]),
('Product', ['GOVT', 'GOVT', 'GOVT', 'GOVT', 'GOVT', 'GOVT', '', '', '',]),
]
df1 = pd.DataFrame.from_items(df1)
print(df1)
df2 = [('ISIN_CUSIP_CODE', ['US20030NCM11', 'US912810SD19', 'XS1851277969', 'XS1391086987', 'CA064151BL66', 'CA13595ZZ661', ]),
('Product_MRD', ['CORP', 'GOVT', 'CORP', 'CORP','CORP','CORP',]),
]
df2 = pd.DataFrame.from_items(df2)
print(df2)

df1
ISIN_CUSIP_CODE Product
0 US68323ABL70 GOVT
1 9128284D9 GOVT
2 912828W89 GOVT
3 CA135087J470 GOVT
4 CA135087J470 GOVT
5 912796QP7 GOVT
6 US20030NCM11
7 US912810SD19
8 XS1851277969
df2
ISIN_CUSIP_CODE Product_MRD
0 US20030NCM11 CORP
1 US912810SD19 GOVT
2 XS1851277969 CORP
3 XS1391086987 CORP
4 CA064151BL66 CORP
5 CA13595ZZ661 CORP


My map function is not returning the looked up values in df2



df1['Product'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])
print(df1)

ISIN_CUSIP_CODE Product
0 US68323ABL70 GOVT
1 9128284D9 GOVT
2 912828W89 GOVT
3 CA135087J470 GOVT
4 CA135087J470 GOVT
5 912796QP7 GOVT
6 US20030NCM11
7 US912810SD19
8 XS1851277969









share|improve this question






















  • Typo: use df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])

    – Sandeep Kadapa
    Nov 16 '18 at 7:52











  • Use np.where(df1['Product'].replace('',np.nan).isnull(),df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD']),df1['Product'])

    – Sandeep Kadapa
    Nov 16 '18 at 7:55











  • Getting NameError: name 'np' is not defined. Does np need to be prefixed/

    – Peter Lucas
    Nov 16 '18 at 7:57











  • import numpy as np

    – Sandeep Kadapa
    Nov 16 '18 at 7:58











  • Sorry same result

    – Peter Lucas
    Nov 16 '18 at 8:01













0












0








0








I'd like to use the map function to update values in df1 based on the looked up value in df2The lookup column is ISIN_CUSIP_CODE



df1 = [('ISIN_CUSIP_CODE', ['US68323ABL70', '9128284D9', '912828W89', 'CA135087J470','CA135087J470','912796QP7','US20030NCM11','US912810SD19','XS1851277969',]),
('Product', ['GOVT', 'GOVT', 'GOVT', 'GOVT', 'GOVT', 'GOVT', '', '', '',]),
]
df1 = pd.DataFrame.from_items(df1)
print(df1)
df2 = [('ISIN_CUSIP_CODE', ['US20030NCM11', 'US912810SD19', 'XS1851277969', 'XS1391086987', 'CA064151BL66', 'CA13595ZZ661', ]),
('Product_MRD', ['CORP', 'GOVT', 'CORP', 'CORP','CORP','CORP',]),
]
df2 = pd.DataFrame.from_items(df2)
print(df2)

df1
ISIN_CUSIP_CODE Product
0 US68323ABL70 GOVT
1 9128284D9 GOVT
2 912828W89 GOVT
3 CA135087J470 GOVT
4 CA135087J470 GOVT
5 912796QP7 GOVT
6 US20030NCM11
7 US912810SD19
8 XS1851277969
df2
ISIN_CUSIP_CODE Product_MRD
0 US20030NCM11 CORP
1 US912810SD19 GOVT
2 XS1851277969 CORP
3 XS1391086987 CORP
4 CA064151BL66 CORP
5 CA13595ZZ661 CORP


My map function is not returning the looked up values in df2



df1['Product'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])
print(df1)

ISIN_CUSIP_CODE Product
0 US68323ABL70 GOVT
1 9128284D9 GOVT
2 912828W89 GOVT
3 CA135087J470 GOVT
4 CA135087J470 GOVT
5 912796QP7 GOVT
6 US20030NCM11
7 US912810SD19
8 XS1851277969









share|improve this question














I'd like to use the map function to update values in df1 based on the looked up value in df2The lookup column is ISIN_CUSIP_CODE



df1 = [('ISIN_CUSIP_CODE', ['US68323ABL70', '9128284D9', '912828W89', 'CA135087J470','CA135087J470','912796QP7','US20030NCM11','US912810SD19','XS1851277969',]),
('Product', ['GOVT', 'GOVT', 'GOVT', 'GOVT', 'GOVT', 'GOVT', '', '', '',]),
]
df1 = pd.DataFrame.from_items(df1)
print(df1)
df2 = [('ISIN_CUSIP_CODE', ['US20030NCM11', 'US912810SD19', 'XS1851277969', 'XS1391086987', 'CA064151BL66', 'CA13595ZZ661', ]),
('Product_MRD', ['CORP', 'GOVT', 'CORP', 'CORP','CORP','CORP',]),
]
df2 = pd.DataFrame.from_items(df2)
print(df2)

df1
ISIN_CUSIP_CODE Product
0 US68323ABL70 GOVT
1 9128284D9 GOVT
2 912828W89 GOVT
3 CA135087J470 GOVT
4 CA135087J470 GOVT
5 912796QP7 GOVT
6 US20030NCM11
7 US912810SD19
8 XS1851277969
df2
ISIN_CUSIP_CODE Product_MRD
0 US20030NCM11 CORP
1 US912810SD19 GOVT
2 XS1851277969 CORP
3 XS1391086987 CORP
4 CA064151BL66 CORP
5 CA13595ZZ661 CORP


My map function is not returning the looked up values in df2



df1['Product'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])
print(df1)

ISIN_CUSIP_CODE Product
0 US68323ABL70 GOVT
1 9128284D9 GOVT
2 912828W89 GOVT
3 CA135087J470 GOVT
4 CA135087J470 GOVT
5 912796QP7 GOVT
6 US20030NCM11
7 US912810SD19
8 XS1851277969






python pandas dataframe






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 16 '18 at 7:43









Peter LucasPeter Lucas

413312




413312












  • Typo: use df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])

    – Sandeep Kadapa
    Nov 16 '18 at 7:52











  • Use np.where(df1['Product'].replace('',np.nan).isnull(),df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD']),df1['Product'])

    – Sandeep Kadapa
    Nov 16 '18 at 7:55











  • Getting NameError: name 'np' is not defined. Does np need to be prefixed/

    – Peter Lucas
    Nov 16 '18 at 7:57











  • import numpy as np

    – Sandeep Kadapa
    Nov 16 '18 at 7:58











  • Sorry same result

    – Peter Lucas
    Nov 16 '18 at 8:01

















  • Typo: use df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])

    – Sandeep Kadapa
    Nov 16 '18 at 7:52











  • Use np.where(df1['Product'].replace('',np.nan).isnull(),df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD']),df1['Product'])

    – Sandeep Kadapa
    Nov 16 '18 at 7:55











  • Getting NameError: name 'np' is not defined. Does np need to be prefixed/

    – Peter Lucas
    Nov 16 '18 at 7:57











  • import numpy as np

    – Sandeep Kadapa
    Nov 16 '18 at 7:58











  • Sorry same result

    – Peter Lucas
    Nov 16 '18 at 8:01
















Typo: use df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])

– Sandeep Kadapa
Nov 16 '18 at 7:52





Typo: use df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])

– Sandeep Kadapa
Nov 16 '18 at 7:52













Use np.where(df1['Product'].replace('',np.nan).isnull(),df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD']),df1['Product'])

– Sandeep Kadapa
Nov 16 '18 at 7:55





Use np.where(df1['Product'].replace('',np.nan).isnull(),df1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD']),df1['Product'])

– Sandeep Kadapa
Nov 16 '18 at 7:55













Getting NameError: name 'np' is not defined. Does np need to be prefixed/

– Peter Lucas
Nov 16 '18 at 7:57





Getting NameError: name 'np' is not defined. Does np need to be prefixed/

– Peter Lucas
Nov 16 '18 at 7:57













import numpy as np

– Sandeep Kadapa
Nov 16 '18 at 7:58





import numpy as np

– Sandeep Kadapa
Nov 16 '18 at 7:58













Sorry same result

– Peter Lucas
Nov 16 '18 at 8:01





Sorry same result

– Peter Lucas
Nov 16 '18 at 8:01












2 Answers
2






active

oldest

votes


















1














This is a simple solution using partial.



from functools import partial
def lookup(row, lookup_df):
try:
return lookup_df[lookup_df.ISIN_CUSIP_CODE == row['ISIN_CUSIP_CODE']].Product_MRD.values[0]
except:
return row['Product']
df1['ProductLooked'] = df1.apply(partial(lookup, lookup_df=df2), axis=1)





share|improve this answer























  • Thanks @Federico Pucci. Was not aware of this library at all. Cheers!

    – Peter Lucas
    Nov 16 '18 at 8:33











  • You're welcome! It's quite useful as well in case you need to reuse functions by just changing args.

    – Federico Pucci
    Nov 16 '18 at 8:34


















2














A purely pandas solution:



pd.concat([df1,df2.rename(columns = 'Product_MRD':'Product')]).drop_duplicates(['ISIN_CUSIP_CODE'],keep='last').sort_values('ISIN_CUSIP_CODE')


No extra libraries required






share|improve this answer

























  • Very nice one liner!

    – Datanovice
    Nov 16 '18 at 8:39











  • @Mark Warburton Great!!

    – Peter Lucas
    Nov 16 '18 at 8:52











Your Answer






StackExchange.ifUsing("editor", function ()
StackExchange.using("externalEditor", function ()
StackExchange.using("snippets", function ()
StackExchange.snippets.init();
);
);
, "code-snippets");

StackExchange.ready(function()
var channelOptions =
tags: "".split(" "),
id: "1"
;
initTagRenderer("".split(" "), "".split(" "), channelOptions);

StackExchange.using("externalEditor", function()
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled)
StackExchange.using("snippets", function()
createEditor();
);

else
createEditor();

);

function createEditor()
StackExchange.prepareEditor(
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader:
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
,
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
);



);













draft saved

draft discarded


















StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333441%2fpandas-use-map-function-to-lookup-a-value-in-another-df%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown

























2 Answers
2






active

oldest

votes








2 Answers
2






active

oldest

votes









active

oldest

votes






active

oldest

votes









1














This is a simple solution using partial.



from functools import partial
def lookup(row, lookup_df):
try:
return lookup_df[lookup_df.ISIN_CUSIP_CODE == row['ISIN_CUSIP_CODE']].Product_MRD.values[0]
except:
return row['Product']
df1['ProductLooked'] = df1.apply(partial(lookup, lookup_df=df2), axis=1)





share|improve this answer























  • Thanks @Federico Pucci. Was not aware of this library at all. Cheers!

    – Peter Lucas
    Nov 16 '18 at 8:33











  • You're welcome! It's quite useful as well in case you need to reuse functions by just changing args.

    – Federico Pucci
    Nov 16 '18 at 8:34















1














This is a simple solution using partial.



from functools import partial
def lookup(row, lookup_df):
try:
return lookup_df[lookup_df.ISIN_CUSIP_CODE == row['ISIN_CUSIP_CODE']].Product_MRD.values[0]
except:
return row['Product']
df1['ProductLooked'] = df1.apply(partial(lookup, lookup_df=df2), axis=1)





share|improve this answer























  • Thanks @Federico Pucci. Was not aware of this library at all. Cheers!

    – Peter Lucas
    Nov 16 '18 at 8:33











  • You're welcome! It's quite useful as well in case you need to reuse functions by just changing args.

    – Federico Pucci
    Nov 16 '18 at 8:34













1












1








1







This is a simple solution using partial.



from functools import partial
def lookup(row, lookup_df):
try:
return lookup_df[lookup_df.ISIN_CUSIP_CODE == row['ISIN_CUSIP_CODE']].Product_MRD.values[0]
except:
return row['Product']
df1['ProductLooked'] = df1.apply(partial(lookup, lookup_df=df2), axis=1)





share|improve this answer













This is a simple solution using partial.



from functools import partial
def lookup(row, lookup_df):
try:
return lookup_df[lookup_df.ISIN_CUSIP_CODE == row['ISIN_CUSIP_CODE']].Product_MRD.values[0]
except:
return row['Product']
df1['ProductLooked'] = df1.apply(partial(lookup, lookup_df=df2), axis=1)






share|improve this answer












share|improve this answer



share|improve this answer










answered Nov 16 '18 at 8:28









Federico PucciFederico Pucci

513




513












  • Thanks @Federico Pucci. Was not aware of this library at all. Cheers!

    – Peter Lucas
    Nov 16 '18 at 8:33











  • You're welcome! It's quite useful as well in case you need to reuse functions by just changing args.

    – Federico Pucci
    Nov 16 '18 at 8:34

















  • Thanks @Federico Pucci. Was not aware of this library at all. Cheers!

    – Peter Lucas
    Nov 16 '18 at 8:33











  • You're welcome! It's quite useful as well in case you need to reuse functions by just changing args.

    – Federico Pucci
    Nov 16 '18 at 8:34
















Thanks @Federico Pucci. Was not aware of this library at all. Cheers!

– Peter Lucas
Nov 16 '18 at 8:33





Thanks @Federico Pucci. Was not aware of this library at all. Cheers!

– Peter Lucas
Nov 16 '18 at 8:33













You're welcome! It's quite useful as well in case you need to reuse functions by just changing args.

– Federico Pucci
Nov 16 '18 at 8:34





You're welcome! It's quite useful as well in case you need to reuse functions by just changing args.

– Federico Pucci
Nov 16 '18 at 8:34













2














A purely pandas solution:



pd.concat([df1,df2.rename(columns = 'Product_MRD':'Product')]).drop_duplicates(['ISIN_CUSIP_CODE'],keep='last').sort_values('ISIN_CUSIP_CODE')


No extra libraries required






share|improve this answer

























  • Very nice one liner!

    – Datanovice
    Nov 16 '18 at 8:39











  • @Mark Warburton Great!!

    – Peter Lucas
    Nov 16 '18 at 8:52















2














A purely pandas solution:



pd.concat([df1,df2.rename(columns = 'Product_MRD':'Product')]).drop_duplicates(['ISIN_CUSIP_CODE'],keep='last').sort_values('ISIN_CUSIP_CODE')


No extra libraries required






share|improve this answer

























  • Very nice one liner!

    – Datanovice
    Nov 16 '18 at 8:39











  • @Mark Warburton Great!!

    – Peter Lucas
    Nov 16 '18 at 8:52













2












2








2







A purely pandas solution:



pd.concat([df1,df2.rename(columns = 'Product_MRD':'Product')]).drop_duplicates(['ISIN_CUSIP_CODE'],keep='last').sort_values('ISIN_CUSIP_CODE')


No extra libraries required






share|improve this answer















A purely pandas solution:



pd.concat([df1,df2.rename(columns = 'Product_MRD':'Product')]).drop_duplicates(['ISIN_CUSIP_CODE'],keep='last').sort_values('ISIN_CUSIP_CODE')


No extra libraries required







share|improve this answer














share|improve this answer



share|improve this answer








edited Nov 16 '18 at 8:39

























answered Nov 16 '18 at 8:37









Mark WarburtonMark Warburton

9217




9217












  • Very nice one liner!

    – Datanovice
    Nov 16 '18 at 8:39











  • @Mark Warburton Great!!

    – Peter Lucas
    Nov 16 '18 at 8:52

















  • Very nice one liner!

    – Datanovice
    Nov 16 '18 at 8:39











  • @Mark Warburton Great!!

    – Peter Lucas
    Nov 16 '18 at 8:52
















Very nice one liner!

– Datanovice
Nov 16 '18 at 8:39





Very nice one liner!

– Datanovice
Nov 16 '18 at 8:39













@Mark Warburton Great!!

– Peter Lucas
Nov 16 '18 at 8:52





@Mark Warburton Great!!

– Peter Lucas
Nov 16 '18 at 8:52

















draft saved

draft discarded
















































Thanks for contributing an answer to Stack Overflow!


  • Please be sure to answer the question. Provide details and share your research!

But avoid


  • Asking for help, clarification, or responding to other answers.

  • Making statements based on opinion; back them up with references or personal experience.

To learn more, see our tips on writing great answers.




draft saved


draft discarded














StackExchange.ready(
function ()
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53333441%2fpandas-use-map-function-to-lookup-a-value-in-another-df%23new-answer', 'question_page');

);

Post as a guest















Required, but never shown





















































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown

































Required, but never shown














Required, but never shown












Required, but never shown







Required, but never shown







Popular posts from this blog

Top Tejano songwriter Luis Silva dead of heart attack at 64

政党

天津地下鉄3号線