Pandas: use map function to LOOKUP a value in another df
I'd like to use the map function to update values in df1
based on the looked up value in df2
The 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
|
show 1 more comment
I'd like to use the map function to update values in df1
based on the looked up value in df2
The 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
Typo: usedf1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])
– Sandeep Kadapa
Nov 16 '18 at 7:52
Usenp.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
|
show 1 more comment
I'd like to use the map function to update values in df1
based on the looked up value in df2
The 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
I'd like to use the map function to update values in df1
based on the looked up value in df2
The 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
python pandas dataframe
asked Nov 16 '18 at 7:43
Peter LucasPeter Lucas
411312
411312
Typo: usedf1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])
– Sandeep Kadapa
Nov 16 '18 at 7:52
Usenp.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
|
show 1 more comment
Typo: usedf1['ISIN_CUSIP_CODE'].map(df2.set_index('ISIN_CUSIP_CODE')['Product_MRD'])
– Sandeep Kadapa
Nov 16 '18 at 7:52
Usenp.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
|
show 1 more comment
2 Answers
2
active
oldest
votes
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)
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
add a comment |
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
Very nice one liner!
– Datanovice
Nov 16 '18 at 8:39
@Mark Warburton Great!!
– Peter Lucas
Nov 16 '18 at 8:52
add a comment |
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
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
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)
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
add a comment |
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)
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
add a comment |
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)
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)
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
add a comment |
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
add a comment |
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
Very nice one liner!
– Datanovice
Nov 16 '18 at 8:39
@Mark Warburton Great!!
– Peter Lucas
Nov 16 '18 at 8:52
add a comment |
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
Very nice one liner!
– Datanovice
Nov 16 '18 at 8:39
@Mark Warburton Great!!
– Peter Lucas
Nov 16 '18 at 8:52
add a comment |
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
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
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
add a comment |
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
add a comment |
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.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
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
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
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
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