Pandas Group by Values and Merge Rows Announcing the arrival of Valued Associate #679: Cesar Manara Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern) Data science time! April 2019 and salary with experience Should we burninate the [wrap] tag? The Ask Question Wizard is Live!How to merge two dictionaries in a single expression?How do I sort a dictionary by value?Add one row to pandas DataFrameRenaming columns in pandasDelete column from pandas DataFrame by column nameHow to drop rows of Pandas DataFrame whose value in certain columns is NaNHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasPandas: Reassigning values in dataframeMerge rows in DataFrame by removing nan's after groupby

List *all* the tuples!

Bonus calculation: Am I making a mountain out of a molehill?

Can a non-EU citizen traveling with me come with me through the EU passport line?

Why is "Captain Marvel" translated as male in Portugal?

Why constant symbols in a language?

Why is "Consequences inflicted." not a sentence?

Diagram with tikz

Is the address of a local variable a constexpr?

Is 1 ppb equal to 1 μg/kg?

Single word antonym of "flightless"

Why was the term "discrete" used in discrete logarithm?

How much radiation do nuclear physics experiments expose researchers to nowadays?

Models of set theory where not every set can be linearly ordered

iPhone Wallpaper?

Withdrew £2800, but only £2000 shows as withdrawn on online banking; what are my obligations?

Dating a Former Employee

When -s is used with third person singular. What's its use in this context?

How to motivate offshore teams and trust them to deliver?

What causes the vertical darker bands in my photo?

When is phishing education going too far?

What is the musical term for a note that continously plays through a melody?

What are the motives behind Cersei's orders given to Bronn?

Were Kohanim forbidden from serving in King David's army?

What do you call a plan that's an alternative plan in case your initial plan fails?



Pandas Group by Values and Merge Rows



Announcing the arrival of Valued Associate #679: Cesar Manara
Planned maintenance scheduled April 17/18, 2019 at 00:00UTC (8:00pm US/Eastern)
Data science time! April 2019 and salary with experience
Should we burninate the [wrap] tag?
The Ask Question Wizard is Live!How to merge two dictionaries in a single expression?How do I sort a dictionary by value?Add one row to pandas DataFrameRenaming columns in pandasDelete column from pandas DataFrame by column nameHow to drop rows of Pandas DataFrame whose value in certain columns is NaNHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasPandas: Reassigning values in dataframeMerge rows in DataFrame by removing nan's after groupby



.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty height:90px;width:728px;box-sizing:border-box;








4















I have a DataFrame and I want to merge the rows that contain same values



toy = [
[10, 11],
[21, 22],
[11, 15],
[22, 23],
[15, 33]
]

toy = pd.DataFrame(toy, columns = ['ID1', 'ID2'])


 ID1 ID2
0 10 11
1 21 22
2 11 15
3 22 23
4 15 33


What I am hoping to get afterwards is



 0 1 2 3
0 10 11 15 33.0
1 21 22 23 NaN


So merging rows that contain any same value within.



My solution is super NOT elegant, I am seeking for the right way to do this... Recursion? Groupby? Hmm..



#### Feel Free to NOT read this... ###
for k in range(100):
print(k)

merge_df = []
merged_indices = []
for i, row in toy.iterrows():
if i in merged_indices:
continue
cp = toy.copy()
merge_rows = cp[cp.isin(row.values)].dropna(how="all")
merged_indices = merged_indices + list(merge_rows.index)
merge_rows = np.array(toy.iloc[merge_rows.index]).flatten()
merge_rows = np.unique(merge_rows)
merge_df.append(merge_rows)

if toy.shape[0] == len(merge_df):
break
toy = pd.DataFrame(merge_df).copy()









share|improve this question

















  • 1





    BTW nice pic of invoker :-)

    – Wen-Ben
    Mar 8 at 17:32











  • @Wen-Ben haha - thx, I like your Saitama too :-)

    – LYu
    Mar 8 at 17:38

















4















I have a DataFrame and I want to merge the rows that contain same values



toy = [
[10, 11],
[21, 22],
[11, 15],
[22, 23],
[15, 33]
]

toy = pd.DataFrame(toy, columns = ['ID1', 'ID2'])


 ID1 ID2
0 10 11
1 21 22
2 11 15
3 22 23
4 15 33


What I am hoping to get afterwards is



 0 1 2 3
0 10 11 15 33.0
1 21 22 23 NaN


So merging rows that contain any same value within.



My solution is super NOT elegant, I am seeking for the right way to do this... Recursion? Groupby? Hmm..



#### Feel Free to NOT read this... ###
for k in range(100):
print(k)

merge_df = []
merged_indices = []
for i, row in toy.iterrows():
if i in merged_indices:
continue
cp = toy.copy()
merge_rows = cp[cp.isin(row.values)].dropna(how="all")
merged_indices = merged_indices + list(merge_rows.index)
merge_rows = np.array(toy.iloc[merge_rows.index]).flatten()
merge_rows = np.unique(merge_rows)
merge_df.append(merge_rows)

if toy.shape[0] == len(merge_df):
break
toy = pd.DataFrame(merge_df).copy()









share|improve this question

















  • 1





    BTW nice pic of invoker :-)

    – Wen-Ben
    Mar 8 at 17:32











  • @Wen-Ben haha - thx, I like your Saitama too :-)

    – LYu
    Mar 8 at 17:38













4












4








4








I have a DataFrame and I want to merge the rows that contain same values



toy = [
[10, 11],
[21, 22],
[11, 15],
[22, 23],
[15, 33]
]

toy = pd.DataFrame(toy, columns = ['ID1', 'ID2'])


 ID1 ID2
0 10 11
1 21 22
2 11 15
3 22 23
4 15 33


What I am hoping to get afterwards is



 0 1 2 3
0 10 11 15 33.0
1 21 22 23 NaN


So merging rows that contain any same value within.



My solution is super NOT elegant, I am seeking for the right way to do this... Recursion? Groupby? Hmm..



#### Feel Free to NOT read this... ###
for k in range(100):
print(k)

merge_df = []
merged_indices = []
for i, row in toy.iterrows():
if i in merged_indices:
continue
cp = toy.copy()
merge_rows = cp[cp.isin(row.values)].dropna(how="all")
merged_indices = merged_indices + list(merge_rows.index)
merge_rows = np.array(toy.iloc[merge_rows.index]).flatten()
merge_rows = np.unique(merge_rows)
merge_df.append(merge_rows)

if toy.shape[0] == len(merge_df):
break
toy = pd.DataFrame(merge_df).copy()









share|improve this question














I have a DataFrame and I want to merge the rows that contain same values



toy = [
[10, 11],
[21, 22],
[11, 15],
[22, 23],
[15, 33]
]

toy = pd.DataFrame(toy, columns = ['ID1', 'ID2'])


 ID1 ID2
0 10 11
1 21 22
2 11 15
3 22 23
4 15 33


What I am hoping to get afterwards is



 0 1 2 3
0 10 11 15 33.0
1 21 22 23 NaN


So merging rows that contain any same value within.



My solution is super NOT elegant, I am seeking for the right way to do this... Recursion? Groupby? Hmm..



#### Feel Free to NOT read this... ###
for k in range(100):
print(k)

merge_df = []
merged_indices = []
for i, row in toy.iterrows():
if i in merged_indices:
continue
cp = toy.copy()
merge_rows = cp[cp.isin(row.values)].dropna(how="all")
merged_indices = merged_indices + list(merge_rows.index)
merge_rows = np.array(toy.iloc[merge_rows.index]).flatten()
merge_rows = np.unique(merge_rows)
merge_df.append(merge_rows)

if toy.shape[0] == len(merge_df):
break
toy = pd.DataFrame(merge_df).copy()






python pandas






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Mar 8 at 16:34









LYuLYu

1,32711230




1,32711230







  • 1





    BTW nice pic of invoker :-)

    – Wen-Ben
    Mar 8 at 17:32











  • @Wen-Ben haha - thx, I like your Saitama too :-)

    – LYu
    Mar 8 at 17:38












  • 1





    BTW nice pic of invoker :-)

    – Wen-Ben
    Mar 8 at 17:32











  • @Wen-Ben haha - thx, I like your Saitama too :-)

    – LYu
    Mar 8 at 17:38







1




1





BTW nice pic of invoker :-)

– Wen-Ben
Mar 8 at 17:32





BTW nice pic of invoker :-)

– Wen-Ben
Mar 8 at 17:32













@Wen-Ben haha - thx, I like your Saitama too :-)

– LYu
Mar 8 at 17:38





@Wen-Ben haha - thx, I like your Saitama too :-)

– LYu
Mar 8 at 17:38












1 Answer
1






active

oldest

votes


















2














Sounds like a network problems so I using networkx



import networkx as nx 
G=nx.from_pandas_edgelist(toy, 'ID1', 'ID2')
l=list(nx.connected_components(G))
newdf=pd.DataFrame(l)
newdf
Out[896]:
0 1 2 3
0 33 10 11 15.0
1 21 22 23 NaN





share|improve this answer























    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
    );



    );













    draft saved

    draft discarded


















    StackExchange.ready(
    function ()
    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067317%2fpandas-group-by-values-and-merge-rows%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









    2














    Sounds like a network problems so I using networkx



    import networkx as nx 
    G=nx.from_pandas_edgelist(toy, 'ID1', 'ID2')
    l=list(nx.connected_components(G))
    newdf=pd.DataFrame(l)
    newdf
    Out[896]:
    0 1 2 3
    0 33 10 11 15.0
    1 21 22 23 NaN





    share|improve this answer



























      2














      Sounds like a network problems so I using networkx



      import networkx as nx 
      G=nx.from_pandas_edgelist(toy, 'ID1', 'ID2')
      l=list(nx.connected_components(G))
      newdf=pd.DataFrame(l)
      newdf
      Out[896]:
      0 1 2 3
      0 33 10 11 15.0
      1 21 22 23 NaN





      share|improve this answer

























        2












        2








        2







        Sounds like a network problems so I using networkx



        import networkx as nx 
        G=nx.from_pandas_edgelist(toy, 'ID1', 'ID2')
        l=list(nx.connected_components(G))
        newdf=pd.DataFrame(l)
        newdf
        Out[896]:
        0 1 2 3
        0 33 10 11 15.0
        1 21 22 23 NaN





        share|improve this answer













        Sounds like a network problems so I using networkx



        import networkx as nx 
        G=nx.from_pandas_edgelist(toy, 'ID1', 'ID2')
        l=list(nx.connected_components(G))
        newdf=pd.DataFrame(l)
        newdf
        Out[896]:
        0 1 2 3
        0 33 10 11 15.0
        1 21 22 23 NaN






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 8 at 17:31









        Wen-BenWen-Ben

        126k83872




        126k83872





























            draft saved

            draft discarded
















































            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.




            draft saved


            draft discarded














            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f55067317%2fpandas-group-by-values-and-merge-rows%23new-answer', 'question_page');

            );

            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







            Popular posts from this blog

            1928 у кіно

            Захаров Федір Захарович

            Ель Греко