Match Merge with Conditions
$begingroup$
I want to merge two csv's. File1 has ID, Begin, End, Type, Value while File2 has ID, End, Col1, Col2. I want to merge them based upon not only matching but upon criteria (if End<=End, merge Col1 and Col2 onto File1; they don't have the same number of rows). I'm not a strong Python user but starting to learn it more and more. Mostly I used R, Excel, SAS, JMP, perl if I needed to do something like this but I want to do it with python...please help. I assume it has something to do with Pandas
# Load the libraries with alias 'pd'
import pandas as pd
import numpy as np
# Read data from file 'filename.csv'
file1 = pd.read_csv("file1.csv")
file2 = pd.read_csv("file2.csv")
# Preview the first 5 lines of the loaded data
file1.head()
file2.head()
Here is the first few lines of file1 and file2:
File1:
ID From To Type Value
1 340 345 RC 0.1
2 345 350 RC 0.15
3 350 355 CR 0.18
File2:
ID Depth Value1 Value2
1 0 0 -90
1 340 0 -270
2 0 0 -60
2 500 0 -75
2 750 0 -55
3 0 0 -90
3 100 30 -270
Output Requested:
File2:
ID Depth Value1 Value2 Type
1 0 0 -90 RC
1 340 0 -270 RC
2 0 0 -60 RC
2 500 0 -75 RC
2 750 0 -55 RC
3 0 0 -90 RC
3 100 30 -270 RC
3 355 30 -250 CR
Please give me a nudge in the right direction. The issue is the To<=Depth.
Thanks in Advance!
I use Python 2.7/Anaconda 2
python
New contributor
$endgroup$
add a comment |
$begingroup$
I want to merge two csv's. File1 has ID, Begin, End, Type, Value while File2 has ID, End, Col1, Col2. I want to merge them based upon not only matching but upon criteria (if End<=End, merge Col1 and Col2 onto File1; they don't have the same number of rows). I'm not a strong Python user but starting to learn it more and more. Mostly I used R, Excel, SAS, JMP, perl if I needed to do something like this but I want to do it with python...please help. I assume it has something to do with Pandas
# Load the libraries with alias 'pd'
import pandas as pd
import numpy as np
# Read data from file 'filename.csv'
file1 = pd.read_csv("file1.csv")
file2 = pd.read_csv("file2.csv")
# Preview the first 5 lines of the loaded data
file1.head()
file2.head()
Here is the first few lines of file1 and file2:
File1:
ID From To Type Value
1 340 345 RC 0.1
2 345 350 RC 0.15
3 350 355 CR 0.18
File2:
ID Depth Value1 Value2
1 0 0 -90
1 340 0 -270
2 0 0 -60
2 500 0 -75
2 750 0 -55
3 0 0 -90
3 100 30 -270
Output Requested:
File2:
ID Depth Value1 Value2 Type
1 0 0 -90 RC
1 340 0 -270 RC
2 0 0 -60 RC
2 500 0 -75 RC
2 750 0 -55 RC
3 0 0 -90 RC
3 100 30 -270 RC
3 355 30 -250 CR
Please give me a nudge in the right direction. The issue is the To<=Depth.
Thanks in Advance!
I use Python 2.7/Anaconda 2
python
New contributor
$endgroup$
1
$begingroup$
I'm sorry we don't help users implement features. We review working code in an effort to improve it. This might help you. Once you have it working bring it back and we can help you make it better.
$endgroup$
– bruglesco
3 hours ago
add a comment |
$begingroup$
I want to merge two csv's. File1 has ID, Begin, End, Type, Value while File2 has ID, End, Col1, Col2. I want to merge them based upon not only matching but upon criteria (if End<=End, merge Col1 and Col2 onto File1; they don't have the same number of rows). I'm not a strong Python user but starting to learn it more and more. Mostly I used R, Excel, SAS, JMP, perl if I needed to do something like this but I want to do it with python...please help. I assume it has something to do with Pandas
# Load the libraries with alias 'pd'
import pandas as pd
import numpy as np
# Read data from file 'filename.csv'
file1 = pd.read_csv("file1.csv")
file2 = pd.read_csv("file2.csv")
# Preview the first 5 lines of the loaded data
file1.head()
file2.head()
Here is the first few lines of file1 and file2:
File1:
ID From To Type Value
1 340 345 RC 0.1
2 345 350 RC 0.15
3 350 355 CR 0.18
File2:
ID Depth Value1 Value2
1 0 0 -90
1 340 0 -270
2 0 0 -60
2 500 0 -75
2 750 0 -55
3 0 0 -90
3 100 30 -270
Output Requested:
File2:
ID Depth Value1 Value2 Type
1 0 0 -90 RC
1 340 0 -270 RC
2 0 0 -60 RC
2 500 0 -75 RC
2 750 0 -55 RC
3 0 0 -90 RC
3 100 30 -270 RC
3 355 30 -250 CR
Please give me a nudge in the right direction. The issue is the To<=Depth.
Thanks in Advance!
I use Python 2.7/Anaconda 2
python
New contributor
$endgroup$
I want to merge two csv's. File1 has ID, Begin, End, Type, Value while File2 has ID, End, Col1, Col2. I want to merge them based upon not only matching but upon criteria (if End<=End, merge Col1 and Col2 onto File1; they don't have the same number of rows). I'm not a strong Python user but starting to learn it more and more. Mostly I used R, Excel, SAS, JMP, perl if I needed to do something like this but I want to do it with python...please help. I assume it has something to do with Pandas
# Load the libraries with alias 'pd'
import pandas as pd
import numpy as np
# Read data from file 'filename.csv'
file1 = pd.read_csv("file1.csv")
file2 = pd.read_csv("file2.csv")
# Preview the first 5 lines of the loaded data
file1.head()
file2.head()
Here is the first few lines of file1 and file2:
File1:
ID From To Type Value
1 340 345 RC 0.1
2 345 350 RC 0.15
3 350 355 CR 0.18
File2:
ID Depth Value1 Value2
1 0 0 -90
1 340 0 -270
2 0 0 -60
2 500 0 -75
2 750 0 -55
3 0 0 -90
3 100 30 -270
Output Requested:
File2:
ID Depth Value1 Value2 Type
1 0 0 -90 RC
1 340 0 -270 RC
2 0 0 -60 RC
2 500 0 -75 RC
2 750 0 -55 RC
3 0 0 -90 RC
3 100 30 -270 RC
3 355 30 -250 CR
Please give me a nudge in the right direction. The issue is the To<=Depth.
Thanks in Advance!
I use Python 2.7/Anaconda 2
python
python
New contributor
New contributor
New contributor
asked 4 hours ago
Celeste WilsonCeleste Wilson
1
1
New contributor
New contributor
1
$begingroup$
I'm sorry we don't help users implement features. We review working code in an effort to improve it. This might help you. Once you have it working bring it back and we can help you make it better.
$endgroup$
– bruglesco
3 hours ago
add a comment |
1
$begingroup$
I'm sorry we don't help users implement features. We review working code in an effort to improve it. This might help you. Once you have it working bring it back and we can help you make it better.
$endgroup$
– bruglesco
3 hours ago
1
1
$begingroup$
I'm sorry we don't help users implement features. We review working code in an effort to improve it. This might help you. Once you have it working bring it back and we can help you make it better.
$endgroup$
– bruglesco
3 hours ago
$begingroup$
I'm sorry we don't help users implement features. We review working code in an effort to improve it. This might help you. Once you have it working bring it back and we can help you make it better.
$endgroup$
– bruglesco
3 hours ago
add a comment |
0
active
oldest
votes
Your Answer
StackExchange.ifUsing("editor", function () {
return StackExchange.using("mathjaxEditing", function () {
StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix) {
StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["\$", "\$"]]);
});
});
}, "mathjax-editing");
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: "196"
};
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: false,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: null,
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
});
}
});
Celeste Wilson is a new contributor. Be nice, and check out our Code of Conduct.
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%2fcodereview.stackexchange.com%2fquestions%2f211956%2fmatch-merge-with-conditions%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
0
active
oldest
votes
0
active
oldest
votes
active
oldest
votes
active
oldest
votes
Celeste Wilson is a new contributor. Be nice, and check out our Code of Conduct.
Celeste Wilson is a new contributor. Be nice, and check out our Code of Conduct.
Celeste Wilson is a new contributor. Be nice, and check out our Code of Conduct.
Celeste Wilson is a new contributor. Be nice, and check out our Code of Conduct.
Thanks for contributing an answer to Code Review Stack Exchange!
- 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.
Use MathJax to format equations. MathJax reference.
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%2fcodereview.stackexchange.com%2fquestions%2f211956%2fmatch-merge-with-conditions%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
1
$begingroup$
I'm sorry we don't help users implement features. We review working code in an effort to improve it. This might help you. Once you have it working bring it back and we can help you make it better.
$endgroup$
– bruglesco
3 hours ago