efficient way to get the adjacency list of a network?
Consider this simple example
pd.DataFrame('id' : [1,1,2,3,4],
'place' : ['bar','pool','bar','kitchen','bar'])
Out[4]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
4 4 bar
Here the network structure is such that a given id
is connected to another id
if they went to the same place.
For instance, here 1
is connected to 2
and 4
because they are at the bar
.
1
and 3
are NOT connected because 1
went to bar
and pool
which does not include kitchen
(the only place 3
went to)
My real data is huge, about 500k. What is the most efficient way to proceed to get the adjacency list
? Here this is just a string with the format source target target
like in https://networkx.github.io/documentation/networkx-1.10/reference/readwrite.adjlist.html
adjacency_list
1 2 4
2 1 4
4 1 2
Can we avoid loops and use Pandas tricks?
Thanks!
python pandas networkx
|
show 2 more comments
Consider this simple example
pd.DataFrame('id' : [1,1,2,3,4],
'place' : ['bar','pool','bar','kitchen','bar'])
Out[4]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
4 4 bar
Here the network structure is such that a given id
is connected to another id
if they went to the same place.
For instance, here 1
is connected to 2
and 4
because they are at the bar
.
1
and 3
are NOT connected because 1
went to bar
and pool
which does not include kitchen
(the only place 3
went to)
My real data is huge, about 500k. What is the most efficient way to proceed to get the adjacency list
? Here this is just a string with the format source target target
like in https://networkx.github.io/documentation/networkx-1.10/reference/readwrite.adjlist.html
adjacency_list
1 2 4
2 1 4
4 1 2
Can we avoid loops and use Pandas tricks?
Thanks!
python pandas networkx
so if more than 2 id went to the same place, do we place them under the same edge_df row?
– Rocky Li
Nov 12 '18 at 2:22
thanks for the comment. I meant theadjacency list
indeed. let me edit the question
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:26
for example , 1 --b 1--c 2--d 3--c 3--d will 1 and 2 connected ?
– W-B
Nov 12 '18 at 2:30
@W-B I dont think so. 1 only went to be and c while 2 went to d
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:37
df.groupby('place').id.unique()?
– W-B
Nov 12 '18 at 2:39
|
show 2 more comments
Consider this simple example
pd.DataFrame('id' : [1,1,2,3,4],
'place' : ['bar','pool','bar','kitchen','bar'])
Out[4]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
4 4 bar
Here the network structure is such that a given id
is connected to another id
if they went to the same place.
For instance, here 1
is connected to 2
and 4
because they are at the bar
.
1
and 3
are NOT connected because 1
went to bar
and pool
which does not include kitchen
(the only place 3
went to)
My real data is huge, about 500k. What is the most efficient way to proceed to get the adjacency list
? Here this is just a string with the format source target target
like in https://networkx.github.io/documentation/networkx-1.10/reference/readwrite.adjlist.html
adjacency_list
1 2 4
2 1 4
4 1 2
Can we avoid loops and use Pandas tricks?
Thanks!
python pandas networkx
Consider this simple example
pd.DataFrame('id' : [1,1,2,3,4],
'place' : ['bar','pool','bar','kitchen','bar'])
Out[4]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
4 4 bar
Here the network structure is such that a given id
is connected to another id
if they went to the same place.
For instance, here 1
is connected to 2
and 4
because they are at the bar
.
1
and 3
are NOT connected because 1
went to bar
and pool
which does not include kitchen
(the only place 3
went to)
My real data is huge, about 500k. What is the most efficient way to proceed to get the adjacency list
? Here this is just a string with the format source target target
like in https://networkx.github.io/documentation/networkx-1.10/reference/readwrite.adjlist.html
adjacency_list
1 2 4
2 1 4
4 1 2
Can we avoid loops and use Pandas tricks?
Thanks!
python pandas networkx
python pandas networkx
edited Nov 12 '18 at 2:57
asked Nov 12 '18 at 2:17
ℕʘʘḆḽḘ
6,819943101
6,819943101
so if more than 2 id went to the same place, do we place them under the same edge_df row?
– Rocky Li
Nov 12 '18 at 2:22
thanks for the comment. I meant theadjacency list
indeed. let me edit the question
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:26
for example , 1 --b 1--c 2--d 3--c 3--d will 1 and 2 connected ?
– W-B
Nov 12 '18 at 2:30
@W-B I dont think so. 1 only went to be and c while 2 went to d
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:37
df.groupby('place').id.unique()?
– W-B
Nov 12 '18 at 2:39
|
show 2 more comments
so if more than 2 id went to the same place, do we place them under the same edge_df row?
– Rocky Li
Nov 12 '18 at 2:22
thanks for the comment. I meant theadjacency list
indeed. let me edit the question
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:26
for example , 1 --b 1--c 2--d 3--c 3--d will 1 and 2 connected ?
– W-B
Nov 12 '18 at 2:30
@W-B I dont think so. 1 only went to be and c while 2 went to d
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:37
df.groupby('place').id.unique()?
– W-B
Nov 12 '18 at 2:39
so if more than 2 id went to the same place, do we place them under the same edge_df row?
– Rocky Li
Nov 12 '18 at 2:22
so if more than 2 id went to the same place, do we place them under the same edge_df row?
– Rocky Li
Nov 12 '18 at 2:22
thanks for the comment. I meant the
adjacency list
indeed. let me edit the question– ℕʘʘḆḽḘ
Nov 12 '18 at 2:26
thanks for the comment. I meant the
adjacency list
indeed. let me edit the question– ℕʘʘḆḽḘ
Nov 12 '18 at 2:26
for example , 1 --b 1--c 2--d 3--c 3--d will 1 and 2 connected ?
– W-B
Nov 12 '18 at 2:30
for example , 1 --b 1--c 2--d 3--c 3--d will 1 and 2 connected ?
– W-B
Nov 12 '18 at 2:30
@W-B I dont think so. 1 only went to be and c while 2 went to d
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:37
@W-B I dont think so. 1 only went to be and c while 2 went to d
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:37
df.groupby('place').id.unique()?
– W-B
Nov 12 '18 at 2:39
df.groupby('place').id.unique()?
– W-B
Nov 12 '18 at 2:39
|
show 2 more comments
2 Answers
2
active
oldest
votes
Using unique
then switch the column 0 to 1 and column 1 to 0 concat
the both df together
adj=pd.DataFrame(df.groupby('place').id.unique().loc[lambda x : x.str.len()>1].tolist())
pd.concat([adj,adj.rename(columns=0:1,1:0)])
Out[810]:
0 1
0 1 2
0 2 1
Update :
newdf=df.merge(df,on='place')
x=nx.from_pandas_dataframe(newdf,'id_x','id_y') # using merge to get the connect for all id by link columns place.
[list(itertools.permutations(x, len(x)) for x in list(nx.connected_components(x))] # using permutations get the all combination for each connected_components in networkx
Out[821]: [[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]]
Data input
df
Out[822]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 bar
nice but would that work with multiple targets for the same source?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:44
@ℕʘʘḆḽḘ check the update
– W-B
Nov 12 '18 at 2:53
sounds promising. do you mind explaining a bit whats going on with the update? thanks!
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:55
@ℕʘʘḆḽḘ added , since if we need to create the network we need two column , you only have one column id and another is the link place ,we using the link to create another missing column , then we can using networkx
– W-B
Nov 12 '18 at 2:59
well done bro, well done
– ℕʘʘḆḽḘ
Nov 12 '18 at 3:10
add a comment |
What about:
>>> df
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
>>> df.groupby('place').id.nunique().value_counts()
1 2
2 1
Name: id, dtype: int64
not sure I understand whats going on here?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:52
@ℕʘʘḆḽḘ, W-B's answer is good.
– pygo
Nov 12 '18 at 2:54
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%2f53255222%2fefficient-way-to-get-the-adjacency-list-of-a-network%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
Using unique
then switch the column 0 to 1 and column 1 to 0 concat
the both df together
adj=pd.DataFrame(df.groupby('place').id.unique().loc[lambda x : x.str.len()>1].tolist())
pd.concat([adj,adj.rename(columns=0:1,1:0)])
Out[810]:
0 1
0 1 2
0 2 1
Update :
newdf=df.merge(df,on='place')
x=nx.from_pandas_dataframe(newdf,'id_x','id_y') # using merge to get the connect for all id by link columns place.
[list(itertools.permutations(x, len(x)) for x in list(nx.connected_components(x))] # using permutations get the all combination for each connected_components in networkx
Out[821]: [[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]]
Data input
df
Out[822]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 bar
nice but would that work with multiple targets for the same source?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:44
@ℕʘʘḆḽḘ check the update
– W-B
Nov 12 '18 at 2:53
sounds promising. do you mind explaining a bit whats going on with the update? thanks!
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:55
@ℕʘʘḆḽḘ added , since if we need to create the network we need two column , you only have one column id and another is the link place ,we using the link to create another missing column , then we can using networkx
– W-B
Nov 12 '18 at 2:59
well done bro, well done
– ℕʘʘḆḽḘ
Nov 12 '18 at 3:10
add a comment |
Using unique
then switch the column 0 to 1 and column 1 to 0 concat
the both df together
adj=pd.DataFrame(df.groupby('place').id.unique().loc[lambda x : x.str.len()>1].tolist())
pd.concat([adj,adj.rename(columns=0:1,1:0)])
Out[810]:
0 1
0 1 2
0 2 1
Update :
newdf=df.merge(df,on='place')
x=nx.from_pandas_dataframe(newdf,'id_x','id_y') # using merge to get the connect for all id by link columns place.
[list(itertools.permutations(x, len(x)) for x in list(nx.connected_components(x))] # using permutations get the all combination for each connected_components in networkx
Out[821]: [[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]]
Data input
df
Out[822]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 bar
nice but would that work with multiple targets for the same source?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:44
@ℕʘʘḆḽḘ check the update
– W-B
Nov 12 '18 at 2:53
sounds promising. do you mind explaining a bit whats going on with the update? thanks!
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:55
@ℕʘʘḆḽḘ added , since if we need to create the network we need two column , you only have one column id and another is the link place ,we using the link to create another missing column , then we can using networkx
– W-B
Nov 12 '18 at 2:59
well done bro, well done
– ℕʘʘḆḽḘ
Nov 12 '18 at 3:10
add a comment |
Using unique
then switch the column 0 to 1 and column 1 to 0 concat
the both df together
adj=pd.DataFrame(df.groupby('place').id.unique().loc[lambda x : x.str.len()>1].tolist())
pd.concat([adj,adj.rename(columns=0:1,1:0)])
Out[810]:
0 1
0 1 2
0 2 1
Update :
newdf=df.merge(df,on='place')
x=nx.from_pandas_dataframe(newdf,'id_x','id_y') # using merge to get the connect for all id by link columns place.
[list(itertools.permutations(x, len(x)) for x in list(nx.connected_components(x))] # using permutations get the all combination for each connected_components in networkx
Out[821]: [[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]]
Data input
df
Out[822]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 bar
Using unique
then switch the column 0 to 1 and column 1 to 0 concat
the both df together
adj=pd.DataFrame(df.groupby('place').id.unique().loc[lambda x : x.str.len()>1].tolist())
pd.concat([adj,adj.rename(columns=0:1,1:0)])
Out[810]:
0 1
0 1 2
0 2 1
Update :
newdf=df.merge(df,on='place')
x=nx.from_pandas_dataframe(newdf,'id_x','id_y') # using merge to get the connect for all id by link columns place.
[list(itertools.permutations(x, len(x)) for x in list(nx.connected_components(x))] # using permutations get the all combination for each connected_components in networkx
Out[821]: [[(1, 2), (1, 3), (2, 1), (2, 3), (3, 1), (3, 2)]]
Data input
df
Out[822]:
id place
0 1 bar
1 1 pool
2 2 bar
3 3 bar
edited Nov 12 '18 at 2:56
answered Nov 12 '18 at 2:42
W-B
102k73163
102k73163
nice but would that work with multiple targets for the same source?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:44
@ℕʘʘḆḽḘ check the update
– W-B
Nov 12 '18 at 2:53
sounds promising. do you mind explaining a bit whats going on with the update? thanks!
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:55
@ℕʘʘḆḽḘ added , since if we need to create the network we need two column , you only have one column id and another is the link place ,we using the link to create another missing column , then we can using networkx
– W-B
Nov 12 '18 at 2:59
well done bro, well done
– ℕʘʘḆḽḘ
Nov 12 '18 at 3:10
add a comment |
nice but would that work with multiple targets for the same source?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:44
@ℕʘʘḆḽḘ check the update
– W-B
Nov 12 '18 at 2:53
sounds promising. do you mind explaining a bit whats going on with the update? thanks!
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:55
@ℕʘʘḆḽḘ added , since if we need to create the network we need two column , you only have one column id and another is the link place ,we using the link to create another missing column , then we can using networkx
– W-B
Nov 12 '18 at 2:59
well done bro, well done
– ℕʘʘḆḽḘ
Nov 12 '18 at 3:10
nice but would that work with multiple targets for the same source?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:44
nice but would that work with multiple targets for the same source?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:44
@ℕʘʘḆḽḘ check the update
– W-B
Nov 12 '18 at 2:53
@ℕʘʘḆḽḘ check the update
– W-B
Nov 12 '18 at 2:53
sounds promising. do you mind explaining a bit whats going on with the update? thanks!
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:55
sounds promising. do you mind explaining a bit whats going on with the update? thanks!
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:55
@ℕʘʘḆḽḘ added , since if we need to create the network we need two column , you only have one column id and another is the link place ,we using the link to create another missing column , then we can using networkx
– W-B
Nov 12 '18 at 2:59
@ℕʘʘḆḽḘ added , since if we need to create the network we need two column , you only have one column id and another is the link place ,we using the link to create another missing column , then we can using networkx
– W-B
Nov 12 '18 at 2:59
well done bro, well done
– ℕʘʘḆḽḘ
Nov 12 '18 at 3:10
well done bro, well done
– ℕʘʘḆḽḘ
Nov 12 '18 at 3:10
add a comment |
What about:
>>> df
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
>>> df.groupby('place').id.nunique().value_counts()
1 2
2 1
Name: id, dtype: int64
not sure I understand whats going on here?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:52
@ℕʘʘḆḽḘ, W-B's answer is good.
– pygo
Nov 12 '18 at 2:54
add a comment |
What about:
>>> df
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
>>> df.groupby('place').id.nunique().value_counts()
1 2
2 1
Name: id, dtype: int64
not sure I understand whats going on here?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:52
@ℕʘʘḆḽḘ, W-B's answer is good.
– pygo
Nov 12 '18 at 2:54
add a comment |
What about:
>>> df
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
>>> df.groupby('place').id.nunique().value_counts()
1 2
2 1
Name: id, dtype: int64
What about:
>>> df
id place
0 1 bar
1 1 pool
2 2 bar
3 3 kitchen
>>> df.groupby('place').id.nunique().value_counts()
1 2
2 1
Name: id, dtype: int64
answered Nov 12 '18 at 2:45
pygo
2,2861617
2,2861617
not sure I understand whats going on here?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:52
@ℕʘʘḆḽḘ, W-B's answer is good.
– pygo
Nov 12 '18 at 2:54
add a comment |
not sure I understand whats going on here?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:52
@ℕʘʘḆḽḘ, W-B's answer is good.
– pygo
Nov 12 '18 at 2:54
not sure I understand whats going on here?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:52
not sure I understand whats going on here?
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:52
@ℕʘʘḆḽḘ, W-B's answer is good.
– pygo
Nov 12 '18 at 2:54
@ℕʘʘḆḽḘ, W-B's answer is good.
– pygo
Nov 12 '18 at 2:54
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.
Some of your past answers have not been well-received, and you're in danger of being blocked from answering.
Please pay close attention to the following guidance:
- 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%2f53255222%2fefficient-way-to-get-the-adjacency-list-of-a-network%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
so if more than 2 id went to the same place, do we place them under the same edge_df row?
– Rocky Li
Nov 12 '18 at 2:22
thanks for the comment. I meant the
adjacency list
indeed. let me edit the question– ℕʘʘḆḽḘ
Nov 12 '18 at 2:26
for example , 1 --b 1--c 2--d 3--c 3--d will 1 and 2 connected ?
– W-B
Nov 12 '18 at 2:30
@W-B I dont think so. 1 only went to be and c while 2 went to d
– ℕʘʘḆḽḘ
Nov 12 '18 at 2:37
df.groupby('place').id.unique()?
– W-B
Nov 12 '18 at 2:39