Transform pandas Data Frame to use for MultiLabelBinarizer












1















My question is: How can I transform a Data Frame like this to eventually use it in scikit's MulitLabelBinarizer:



d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D','E']}
df1 = pd.DataFrame(data=d1)
df1

ID km weight label
0 1 80 10 A
1 2 90 20 B
2 2 90 20 C
3 4 100 30 D


It should tourn ot like this:



d2 ={'km':[80,90,100], 'weight':[10,20,30], 'label':['A',('B','C'),'D']}
df2 = pd.DataFrame(data=d2)
df2

km weight label
0 80 10 A
1 90 20 (B, C)
2 100 30 D


So I can juse the data properly in the MultiLabelBinarizer:



from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
mlb.fit(df2['label'])
mlb.transform(df2['label'])

array([[1, 0, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 1]])


Note: the raw data has more than 1 million rows.










share|improve this question




















  • 1





    So what you want is to check for multiple occurrence in a dictionary? Maybe the title is not clear.

    – mikuszefski
    Nov 27 '18 at 7:49











  • yes, excuse me not beeing clear on that.

    – josnas
    Nov 27 '18 at 8:05
















1















My question is: How can I transform a Data Frame like this to eventually use it in scikit's MulitLabelBinarizer:



d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D','E']}
df1 = pd.DataFrame(data=d1)
df1

ID km weight label
0 1 80 10 A
1 2 90 20 B
2 2 90 20 C
3 4 100 30 D


It should tourn ot like this:



d2 ={'km':[80,90,100], 'weight':[10,20,30], 'label':['A',('B','C'),'D']}
df2 = pd.DataFrame(data=d2)
df2

km weight label
0 80 10 A
1 90 20 (B, C)
2 100 30 D


So I can juse the data properly in the MultiLabelBinarizer:



from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
mlb.fit(df2['label'])
mlb.transform(df2['label'])

array([[1, 0, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 1]])


Note: the raw data has more than 1 million rows.










share|improve this question




















  • 1





    So what you want is to check for multiple occurrence in a dictionary? Maybe the title is not clear.

    – mikuszefski
    Nov 27 '18 at 7:49











  • yes, excuse me not beeing clear on that.

    – josnas
    Nov 27 '18 at 8:05














1












1








1








My question is: How can I transform a Data Frame like this to eventually use it in scikit's MulitLabelBinarizer:



d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D','E']}
df1 = pd.DataFrame(data=d1)
df1

ID km weight label
0 1 80 10 A
1 2 90 20 B
2 2 90 20 C
3 4 100 30 D


It should tourn ot like this:



d2 ={'km':[80,90,100], 'weight':[10,20,30], 'label':['A',('B','C'),'D']}
df2 = pd.DataFrame(data=d2)
df2

km weight label
0 80 10 A
1 90 20 (B, C)
2 100 30 D


So I can juse the data properly in the MultiLabelBinarizer:



from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
mlb.fit(df2['label'])
mlb.transform(df2['label'])

array([[1, 0, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 1]])


Note: the raw data has more than 1 million rows.










share|improve this question
















My question is: How can I transform a Data Frame like this to eventually use it in scikit's MulitLabelBinarizer:



d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D','E']}
df1 = pd.DataFrame(data=d1)
df1

ID km weight label
0 1 80 10 A
1 2 90 20 B
2 2 90 20 C
3 4 100 30 D


It should tourn ot like this:



d2 ={'km':[80,90,100], 'weight':[10,20,30], 'label':['A',('B','C'),'D']}
df2 = pd.DataFrame(data=d2)
df2

km weight label
0 80 10 A
1 90 20 (B, C)
2 100 30 D


So I can juse the data properly in the MultiLabelBinarizer:



from sklearn.preprocessing import MultiLabelBinarizer

mlb = MultiLabelBinarizer()
mlb.fit(df2['label'])
mlb.transform(df2['label'])

array([[1, 0, 0, 0],
[0, 1, 1, 0],
[0, 0, 0, 1]])


Note: the raw data has more than 1 million rows.







python dataframe scikit-learn transformation multilabel-classification






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 27 '18 at 8:00







josnas

















asked Nov 27 '18 at 7:41









josnasjosnas

83




83








  • 1





    So what you want is to check for multiple occurrence in a dictionary? Maybe the title is not clear.

    – mikuszefski
    Nov 27 '18 at 7:49











  • yes, excuse me not beeing clear on that.

    – josnas
    Nov 27 '18 at 8:05














  • 1





    So what you want is to check for multiple occurrence in a dictionary? Maybe the title is not clear.

    – mikuszefski
    Nov 27 '18 at 7:49











  • yes, excuse me not beeing clear on that.

    – josnas
    Nov 27 '18 at 8:05








1




1





So what you want is to check for multiple occurrence in a dictionary? Maybe the title is not clear.

– mikuszefski
Nov 27 '18 at 7:49





So what you want is to check for multiple occurrence in a dictionary? Maybe the title is not clear.

– mikuszefski
Nov 27 '18 at 7:49













yes, excuse me not beeing clear on that.

– josnas
Nov 27 '18 at 8:05





yes, excuse me not beeing clear on that.

– josnas
Nov 27 '18 at 8:05












1 Answer
1






active

oldest

votes


















0














I think you need this:



d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D']}
df1 = pd.DataFrame(data=d1)
#Groupby and get tuple, like you need
df2 = pd.DataFrame(df1.groupby(['km','weight'])['label'].apply(lambda x: tuple(x.values)))
df2.reset_index(inplace=True)

from sklearn.preprocessing import MultiLabelBinarizer
mlb = MultiLabelBinarizer()
mlb.fit(df2['label'])
mlb.transform(df2['label'])





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%2f53494873%2ftransform-pandas-data-frame-to-use-for-multilabelbinarizer%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









    0














    I think you need this:



    d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D']}
    df1 = pd.DataFrame(data=d1)
    #Groupby and get tuple, like you need
    df2 = pd.DataFrame(df1.groupby(['km','weight'])['label'].apply(lambda x: tuple(x.values)))
    df2.reset_index(inplace=True)

    from sklearn.preprocessing import MultiLabelBinarizer
    mlb = MultiLabelBinarizer()
    mlb.fit(df2['label'])
    mlb.transform(df2['label'])





    share|improve this answer




























      0














      I think you need this:



      d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D']}
      df1 = pd.DataFrame(data=d1)
      #Groupby and get tuple, like you need
      df2 = pd.DataFrame(df1.groupby(['km','weight'])['label'].apply(lambda x: tuple(x.values)))
      df2.reset_index(inplace=True)

      from sklearn.preprocessing import MultiLabelBinarizer
      mlb = MultiLabelBinarizer()
      mlb.fit(df2['label'])
      mlb.transform(df2['label'])





      share|improve this answer


























        0












        0








        0







        I think you need this:



        d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D']}
        df1 = pd.DataFrame(data=d1)
        #Groupby and get tuple, like you need
        df2 = pd.DataFrame(df1.groupby(['km','weight'])['label'].apply(lambda x: tuple(x.values)))
        df2.reset_index(inplace=True)

        from sklearn.preprocessing import MultiLabelBinarizer
        mlb = MultiLabelBinarizer()
        mlb.fit(df2['label'])
        mlb.transform(df2['label'])





        share|improve this answer













        I think you need this:



        d1 = {'ID':[1,2,3,4], 'km':[80,90,90,100], 'weight':[10,20,20,30], 'label':['A','B','C','D']}
        df1 = pd.DataFrame(data=d1)
        #Groupby and get tuple, like you need
        df2 = pd.DataFrame(df1.groupby(['km','weight'])['label'].apply(lambda x: tuple(x.values)))
        df2.reset_index(inplace=True)

        from sklearn.preprocessing import MultiLabelBinarizer
        mlb = MultiLabelBinarizer()
        mlb.fit(df2['label'])
        mlb.transform(df2['label'])






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 27 '18 at 8:18









        Rudolf MorkovskyiRudolf Morkovskyi

        730117




        730117
































            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%2f53494873%2ftransform-pandas-data-frame-to-use-for-multilabelbinarizer%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

            A CLEAN and SIMPLE way to add appendices to Table of Contents and bookmarks

            Calculate evaluation metrics using cross_val_predict sklearn

            Insert data from modal to MySQL (multiple modal on website)