find reciprocal rows in pandas Dataframe
I have this dataframe, and need to retain only those lines having a reciprocal values for 2 columns (numA and numB here).
gpm = pd.DataFrame(data={
'id':[1,2,3,4,5,6,7,8,9],
'time':[150315,150315,150315,150315,150315,150315,150315,150315,150315],
'numA':['A','D','C','B','A','C','A','E','D'],
'numB':['B','C','B','A','B','D','B','A','A'],
'antA':['MSPDV','VIELU','RMPC1','MJCIH','PALT2','M2PV3','MACIF','MACIF','VIELU'],
'antB':['BPDV8','0GRI3','SSFDJ','SSFDJ','SSFDJ','CCPG1','0GRI3','SSFDJ','SSFDJ']
})
I only want lines in which columns numA and numB are reciprocal. That is, retaining al lines where the pairs (A,B), (B,A) and (C,D),(D,C) occur.
My solution, for now, involves making a list of all unique identifiers and going through each line looking whether the actual partner is in the list of partners
it is extremely slow.... (and perhaps incorrect!)
## here's my code
parties = {}
nums = gpm['numA']+gpm['numB']
for i in nums.unique():
parties[i] = gpm['numB'][gpm['numA'] == i]
parties[i] = gpm['numA'][gpm['numB'] == i]
new_d = gpm.iloc[[0]]
for i in np.arange(1,gpm.shape[0]):
numa = gpm.iloc[i]['numA']
if gpm.iloc[i]['numB'] in parties[numa]:
new_d.append(gpm.iloc[[i]])
any savvy coder that could help speed this up? The actual file to parse is a ~15GB csv.
Thanks
python pandas large-data
add a comment |
I have this dataframe, and need to retain only those lines having a reciprocal values for 2 columns (numA and numB here).
gpm = pd.DataFrame(data={
'id':[1,2,3,4,5,6,7,8,9],
'time':[150315,150315,150315,150315,150315,150315,150315,150315,150315],
'numA':['A','D','C','B','A','C','A','E','D'],
'numB':['B','C','B','A','B','D','B','A','A'],
'antA':['MSPDV','VIELU','RMPC1','MJCIH','PALT2','M2PV3','MACIF','MACIF','VIELU'],
'antB':['BPDV8','0GRI3','SSFDJ','SSFDJ','SSFDJ','CCPG1','0GRI3','SSFDJ','SSFDJ']
})
I only want lines in which columns numA and numB are reciprocal. That is, retaining al lines where the pairs (A,B), (B,A) and (C,D),(D,C) occur.
My solution, for now, involves making a list of all unique identifiers and going through each line looking whether the actual partner is in the list of partners
it is extremely slow.... (and perhaps incorrect!)
## here's my code
parties = {}
nums = gpm['numA']+gpm['numB']
for i in nums.unique():
parties[i] = gpm['numB'][gpm['numA'] == i]
parties[i] = gpm['numA'][gpm['numB'] == i]
new_d = gpm.iloc[[0]]
for i in np.arange(1,gpm.shape[0]):
numa = gpm.iloc[i]['numA']
if gpm.iloc[i]['numB'] in parties[numa]:
new_d.append(gpm.iloc[[i]])
any savvy coder that could help speed this up? The actual file to parse is a ~15GB csv.
Thanks
python pandas large-data
add a comment |
I have this dataframe, and need to retain only those lines having a reciprocal values for 2 columns (numA and numB here).
gpm = pd.DataFrame(data={
'id':[1,2,3,4,5,6,7,8,9],
'time':[150315,150315,150315,150315,150315,150315,150315,150315,150315],
'numA':['A','D','C','B','A','C','A','E','D'],
'numB':['B','C','B','A','B','D','B','A','A'],
'antA':['MSPDV','VIELU','RMPC1','MJCIH','PALT2','M2PV3','MACIF','MACIF','VIELU'],
'antB':['BPDV8','0GRI3','SSFDJ','SSFDJ','SSFDJ','CCPG1','0GRI3','SSFDJ','SSFDJ']
})
I only want lines in which columns numA and numB are reciprocal. That is, retaining al lines where the pairs (A,B), (B,A) and (C,D),(D,C) occur.
My solution, for now, involves making a list of all unique identifiers and going through each line looking whether the actual partner is in the list of partners
it is extremely slow.... (and perhaps incorrect!)
## here's my code
parties = {}
nums = gpm['numA']+gpm['numB']
for i in nums.unique():
parties[i] = gpm['numB'][gpm['numA'] == i]
parties[i] = gpm['numA'][gpm['numB'] == i]
new_d = gpm.iloc[[0]]
for i in np.arange(1,gpm.shape[0]):
numa = gpm.iloc[i]['numA']
if gpm.iloc[i]['numB'] in parties[numa]:
new_d.append(gpm.iloc[[i]])
any savvy coder that could help speed this up? The actual file to parse is a ~15GB csv.
Thanks
python pandas large-data
I have this dataframe, and need to retain only those lines having a reciprocal values for 2 columns (numA and numB here).
gpm = pd.DataFrame(data={
'id':[1,2,3,4,5,6,7,8,9],
'time':[150315,150315,150315,150315,150315,150315,150315,150315,150315],
'numA':['A','D','C','B','A','C','A','E','D'],
'numB':['B','C','B','A','B','D','B','A','A'],
'antA':['MSPDV','VIELU','RMPC1','MJCIH','PALT2','M2PV3','MACIF','MACIF','VIELU'],
'antB':['BPDV8','0GRI3','SSFDJ','SSFDJ','SSFDJ','CCPG1','0GRI3','SSFDJ','SSFDJ']
})
I only want lines in which columns numA and numB are reciprocal. That is, retaining al lines where the pairs (A,B), (B,A) and (C,D),(D,C) occur.
My solution, for now, involves making a list of all unique identifiers and going through each line looking whether the actual partner is in the list of partners
it is extremely slow.... (and perhaps incorrect!)
## here's my code
parties = {}
nums = gpm['numA']+gpm['numB']
for i in nums.unique():
parties[i] = gpm['numB'][gpm['numA'] == i]
parties[i] = gpm['numA'][gpm['numB'] == i]
new_d = gpm.iloc[[0]]
for i in np.arange(1,gpm.shape[0]):
numa = gpm.iloc[i]['numA']
if gpm.iloc[i]['numB'] in parties[numa]:
new_d.append(gpm.iloc[[i]])
any savvy coder that could help speed this up? The actual file to parse is a ~15GB csv.
Thanks
python pandas large-data
python pandas large-data
asked Nov 14 '18 at 12:45
HoracioHoracio
335
335
add a comment |
add a comment |
1 Answer
1
active
oldest
votes
In your example, I assume the rows with id=3, 8 & 9, which are (C, B), (E, A) and (D, A), are unwanted? If so, here's a standard way to select by comparing the values in numA and numB for specific acceptable combinations:
In [5]: gpm[((gpm['numA'] == 'A') & (gpm['numB'] == 'B')) |
...: ((gpm['numA'] == 'B') & (gpm['numB'] == 'A')) |
...: ((gpm['numA'] == 'C') & (gpm['numB'] == 'D')) |
...: ((gpm['numA'] == 'D') & (gpm['numB'] == 'C'))
...: ]
Out[5]:
id time numA numB antA antB
0 1 150315 A B MSPDV BPDV8
1 2 150315 D C VIELU 0GRI3
3 4 150315 B A MJCIH SSFDJ
4 5 150315 A B PALT2 SSFDJ
5 6 150315 C D M2PV3 CCPG1
6 7 150315 A B MACIF 0GRI3
(assign the result of that to new_d)
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%2f53300569%2ffind-reciprocal-rows-in-pandas-dataframe%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
1 Answer
1
active
oldest
votes
1 Answer
1
active
oldest
votes
active
oldest
votes
active
oldest
votes
In your example, I assume the rows with id=3, 8 & 9, which are (C, B), (E, A) and (D, A), are unwanted? If so, here's a standard way to select by comparing the values in numA and numB for specific acceptable combinations:
In [5]: gpm[((gpm['numA'] == 'A') & (gpm['numB'] == 'B')) |
...: ((gpm['numA'] == 'B') & (gpm['numB'] == 'A')) |
...: ((gpm['numA'] == 'C') & (gpm['numB'] == 'D')) |
...: ((gpm['numA'] == 'D') & (gpm['numB'] == 'C'))
...: ]
Out[5]:
id time numA numB antA antB
0 1 150315 A B MSPDV BPDV8
1 2 150315 D C VIELU 0GRI3
3 4 150315 B A MJCIH SSFDJ
4 5 150315 A B PALT2 SSFDJ
5 6 150315 C D M2PV3 CCPG1
6 7 150315 A B MACIF 0GRI3
(assign the result of that to new_d)
add a comment |
In your example, I assume the rows with id=3, 8 & 9, which are (C, B), (E, A) and (D, A), are unwanted? If so, here's a standard way to select by comparing the values in numA and numB for specific acceptable combinations:
In [5]: gpm[((gpm['numA'] == 'A') & (gpm['numB'] == 'B')) |
...: ((gpm['numA'] == 'B') & (gpm['numB'] == 'A')) |
...: ((gpm['numA'] == 'C') & (gpm['numB'] == 'D')) |
...: ((gpm['numA'] == 'D') & (gpm['numB'] == 'C'))
...: ]
Out[5]:
id time numA numB antA antB
0 1 150315 A B MSPDV BPDV8
1 2 150315 D C VIELU 0GRI3
3 4 150315 B A MJCIH SSFDJ
4 5 150315 A B PALT2 SSFDJ
5 6 150315 C D M2PV3 CCPG1
6 7 150315 A B MACIF 0GRI3
(assign the result of that to new_d)
add a comment |
In your example, I assume the rows with id=3, 8 & 9, which are (C, B), (E, A) and (D, A), are unwanted? If so, here's a standard way to select by comparing the values in numA and numB for specific acceptable combinations:
In [5]: gpm[((gpm['numA'] == 'A') & (gpm['numB'] == 'B')) |
...: ((gpm['numA'] == 'B') & (gpm['numB'] == 'A')) |
...: ((gpm['numA'] == 'C') & (gpm['numB'] == 'D')) |
...: ((gpm['numA'] == 'D') & (gpm['numB'] == 'C'))
...: ]
Out[5]:
id time numA numB antA antB
0 1 150315 A B MSPDV BPDV8
1 2 150315 D C VIELU 0GRI3
3 4 150315 B A MJCIH SSFDJ
4 5 150315 A B PALT2 SSFDJ
5 6 150315 C D M2PV3 CCPG1
6 7 150315 A B MACIF 0GRI3
(assign the result of that to new_d)
In your example, I assume the rows with id=3, 8 & 9, which are (C, B), (E, A) and (D, A), are unwanted? If so, here's a standard way to select by comparing the values in numA and numB for specific acceptable combinations:
In [5]: gpm[((gpm['numA'] == 'A') & (gpm['numB'] == 'B')) |
...: ((gpm['numA'] == 'B') & (gpm['numB'] == 'A')) |
...: ((gpm['numA'] == 'C') & (gpm['numB'] == 'D')) |
...: ((gpm['numA'] == 'D') & (gpm['numB'] == 'C'))
...: ]
Out[5]:
id time numA numB antA antB
0 1 150315 A B MSPDV BPDV8
1 2 150315 D C VIELU 0GRI3
3 4 150315 B A MJCIH SSFDJ
4 5 150315 A B PALT2 SSFDJ
5 6 150315 C D M2PV3 CCPG1
6 7 150315 A B MACIF 0GRI3
(assign the result of that to new_d)
answered Nov 14 '18 at 14:25
aneroidaneroid
6,99922742
6,99922742
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%2f53300569%2ffind-reciprocal-rows-in-pandas-dataframe%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