How to compare 3 columns of DataFrame together, Python 3.6
I have below dataframe and I want to compare 3 columns value & update True/False in another column "Id_Name_Table_Matching"
Below my code:
L1_ID = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Name = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Table = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
DF1 = pd.DataFrame({'dimId': L1_ID, 'dimName': L1_Name, 'sqlTableColumn': L1_Table})
I want to update true in "Id_Name_Table_Matching" if all columns value matches else false.
I need script like below:
DF1['Id_Name_Table_Matching'] = DF1['dimId'] == DF1['dimName'] == DF1['sqlTableColumn']
python python-3.x pandas list dataframe
add a comment |
I have below dataframe and I want to compare 3 columns value & update True/False in another column "Id_Name_Table_Matching"
Below my code:
L1_ID = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Name = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Table = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
DF1 = pd.DataFrame({'dimId': L1_ID, 'dimName': L1_Name, 'sqlTableColumn': L1_Table})
I want to update true in "Id_Name_Table_Matching" if all columns value matches else false.
I need script like below:
DF1['Id_Name_Table_Matching'] = DF1['dimId'] == DF1['dimName'] == DF1['sqlTableColumn']
python python-3.x pandas list dataframe
add a comment |
I have below dataframe and I want to compare 3 columns value & update True/False in another column "Id_Name_Table_Matching"
Below my code:
L1_ID = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Name = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Table = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
DF1 = pd.DataFrame({'dimId': L1_ID, 'dimName': L1_Name, 'sqlTableColumn': L1_Table})
I want to update true in "Id_Name_Table_Matching" if all columns value matches else false.
I need script like below:
DF1['Id_Name_Table_Matching'] = DF1['dimId'] == DF1['dimName'] == DF1['sqlTableColumn']
python python-3.x pandas list dataframe
I have below dataframe and I want to compare 3 columns value & update True/False in another column "Id_Name_Table_Matching"
Below my code:
L1_ID = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Name = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
L1_Table = ['Region', 'Col2', 'Col3', 'Col4', 'Col5']
DF1 = pd.DataFrame({'dimId': L1_ID, 'dimName': L1_Name, 'sqlTableColumn': L1_Table})
I want to update true in "Id_Name_Table_Matching" if all columns value matches else false.
I need script like below:
DF1['Id_Name_Table_Matching'] = DF1['dimId'] == DF1['dimName'] == DF1['sqlTableColumn']
python python-3.x pandas list dataframe
python python-3.x pandas list dataframe
edited Nov 13 '18 at 14:10
SPy
asked Nov 13 '18 at 14:06
SPySPy
497519
497519
add a comment |
add a comment |
2 Answers
2
active
oldest
votes
Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:
DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) &
(DF1['dimId'] == DF1['sqlTableColumn'])
General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:
cols = ['dimId','dimName','sqlTableColumn']
DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
print (DF1)
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
Detail:
print (DF1[cols].eq(DF1[cols[0]], axis=0))
dimId dimName sqlTableColumn
0 True True True
1 True True True
2 True True True
3 True True True
4 True True True
1
thanks, First one ok - easy one
– SPy
Nov 13 '18 at 14:20
add a comment |
Look if this helps. Using .apply()
df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
print(df)
Output:
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
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%2f53282823%2fhow-to-compare-3-columns-of-dataframe-together-python-3-6%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
Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:
DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) &
(DF1['dimId'] == DF1['sqlTableColumn'])
General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:
cols = ['dimId','dimName','sqlTableColumn']
DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
print (DF1)
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
Detail:
print (DF1[cols].eq(DF1[cols[0]], axis=0))
dimId dimName sqlTableColumn
0 True True True
1 True True True
2 True True True
3 True True True
4 True True True
1
thanks, First one ok - easy one
– SPy
Nov 13 '18 at 14:20
add a comment |
Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:
DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) &
(DF1['dimId'] == DF1['sqlTableColumn'])
General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:
cols = ['dimId','dimName','sqlTableColumn']
DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
print (DF1)
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
Detail:
print (DF1[cols].eq(DF1[cols[0]], axis=0))
dimId dimName sqlTableColumn
0 True True True
1 True True True
2 True True True
3 True True True
4 True True True
1
thanks, First one ok - easy one
– SPy
Nov 13 '18 at 14:20
add a comment |
Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:
DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) &
(DF1['dimId'] == DF1['sqlTableColumn'])
General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:
cols = ['dimId','dimName','sqlTableColumn']
DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
print (DF1)
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
Detail:
print (DF1[cols].eq(DF1[cols[0]], axis=0))
dimId dimName sqlTableColumn
0 True True True
1 True True True
2 True True True
3 True True True
4 True True True
Compare first columns with second, then with last and chain boolena masks by & for bitwise AND:
DF1['Id_Name_Table_Matching'] = (DF1['dimId'] == DF1['dimName']) &
(DF1['dimId'] == DF1['sqlTableColumn'])
General solution for compare multiple columns defined in list - all filtered columns compare by first one by DataFrame.eq and then check if all values per rows are Trues by DataFrame.all:
cols = ['dimId','dimName','sqlTableColumn']
DF1['Id_Name_Table_Matching'] = DF1[cols].eq(DF1[cols[0]], axis=0).all(axis=1)
print (DF1)
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
Detail:
print (DF1[cols].eq(DF1[cols[0]], axis=0))
dimId dimName sqlTableColumn
0 True True True
1 True True True
2 True True True
3 True True True
4 True True True
edited Nov 13 '18 at 14:17
answered Nov 13 '18 at 14:12
jezraeljezrael
329k23270349
329k23270349
1
thanks, First one ok - easy one
– SPy
Nov 13 '18 at 14:20
add a comment |
1
thanks, First one ok - easy one
– SPy
Nov 13 '18 at 14:20
1
1
thanks, First one ok - easy one
– SPy
Nov 13 '18 at 14:20
thanks, First one ok - easy one
– SPy
Nov 13 '18 at 14:20
add a comment |
Look if this helps. Using .apply()
df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
print(df)
Output:
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
add a comment |
Look if this helps. Using .apply()
df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
print(df)
Output:
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
add a comment |
Look if this helps. Using .apply()
df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
print(df)
Output:
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
Look if this helps. Using .apply()
df["Id_Name_Table_Matching"] = df.apply(lambda x: x.dimId == x.dimName == x.sqlTableColumn, axis = 1)
print(df)
Output:
dimId dimName sqlTableColumn Id_Name_Table_Matching
0 Region Region Region True
1 Col2 Col2 Col2 True
2 Col3 Col3 Col3 True
3 Col4 Col4 Col4 True
4 Col5 Col5 Col5 True
edited Nov 29 '18 at 18:11
answered Nov 13 '18 at 14:38
Srce CdeSrce Cde
1,144511
1,144511
add a comment |
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%2f53282823%2fhow-to-compare-3-columns-of-dataframe-together-python-3-6%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