Converting dataset to Reward/Miss dataset in Python












2















I want to convert the following dataset (CSV) using Pandas and NumPy in Python:



Table 1 (csv)



Ads, Impressions, Clicks
Ad_1, 11, 1
Ad_2, 10, 2


to



Table 2 (csv)



Ad_1, Ad_2
0, 0
0, 0
0, 0
0, 1
0, 0
1, 0
0, 0
0, 0
0, 1
0, 0
0


Table 2 has basically impressions as total number of rows with random insertion of 1's (count = Clicks).



The converted table is to run CTR optimization on 2 Ad sets using Upper Confidence Bound algorithm using machine learning. Kindly help how to convert Table 1 to Table 2.



Thanks!










share|improve this question

























  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed).

    – desertnaut
    Dec 28 '18 at 16:59











  • It was just to call in more help!

    – Vaibhav Magon
    Dec 29 '18 at 10:27











  • That's not a legitimate use of tags (let alone that the python/pandas/numpy community is already big enough) - kindly refrain from the practice next time

    – desertnaut
    Dec 29 '18 at 12:23
















2















I want to convert the following dataset (CSV) using Pandas and NumPy in Python:



Table 1 (csv)



Ads, Impressions, Clicks
Ad_1, 11, 1
Ad_2, 10, 2


to



Table 2 (csv)



Ad_1, Ad_2
0, 0
0, 0
0, 0
0, 1
0, 0
1, 0
0, 0
0, 0
0, 1
0, 0
0


Table 2 has basically impressions as total number of rows with random insertion of 1's (count = Clicks).



The converted table is to run CTR optimization on 2 Ad sets using Upper Confidence Bound algorithm using machine learning. Kindly help how to convert Table 1 to Table 2.



Thanks!










share|improve this question

























  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed).

    – desertnaut
    Dec 28 '18 at 16:59











  • It was just to call in more help!

    – Vaibhav Magon
    Dec 29 '18 at 10:27











  • That's not a legitimate use of tags (let alone that the python/pandas/numpy community is already big enough) - kindly refrain from the practice next time

    – desertnaut
    Dec 29 '18 at 12:23














2












2








2








I want to convert the following dataset (CSV) using Pandas and NumPy in Python:



Table 1 (csv)



Ads, Impressions, Clicks
Ad_1, 11, 1
Ad_2, 10, 2


to



Table 2 (csv)



Ad_1, Ad_2
0, 0
0, 0
0, 0
0, 1
0, 0
1, 0
0, 0
0, 0
0, 1
0, 0
0


Table 2 has basically impressions as total number of rows with random insertion of 1's (count = Clicks).



The converted table is to run CTR optimization on 2 Ad sets using Upper Confidence Bound algorithm using machine learning. Kindly help how to convert Table 1 to Table 2.



Thanks!










share|improve this question
















I want to convert the following dataset (CSV) using Pandas and NumPy in Python:



Table 1 (csv)



Ads, Impressions, Clicks
Ad_1, 11, 1
Ad_2, 10, 2


to



Table 2 (csv)



Ad_1, Ad_2
0, 0
0, 0
0, 0
0, 1
0, 0
1, 0
0, 0
0, 0
0, 1
0, 0
0


Table 2 has basically impressions as total number of rows with random insertion of 1's (count = Clicks).



The converted table is to run CTR optimization on 2 Ad sets using Upper Confidence Bound algorithm using machine learning. Kindly help how to convert Table 1 to Table 2.



Thanks!







python pandas numpy






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Dec 28 '18 at 16:59









desertnaut

16.8k63566




16.8k63566










asked Dec 28 '18 at 13:03









Vaibhav MagonVaibhav Magon

1,0501926




1,0501926













  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed).

    – desertnaut
    Dec 28 '18 at 16:59











  • It was just to call in more help!

    – Vaibhav Magon
    Dec 29 '18 at 10:27











  • That's not a legitimate use of tags (let alone that the python/pandas/numpy community is already big enough) - kindly refrain from the practice next time

    – desertnaut
    Dec 29 '18 at 12:23



















  • Question has nothing to do with machine-learning - kindly do not spam the tag (removed).

    – desertnaut
    Dec 28 '18 at 16:59











  • It was just to call in more help!

    – Vaibhav Magon
    Dec 29 '18 at 10:27











  • That's not a legitimate use of tags (let alone that the python/pandas/numpy community is already big enough) - kindly refrain from the practice next time

    – desertnaut
    Dec 29 '18 at 12:23

















Question has nothing to do with machine-learning - kindly do not spam the tag (removed).

– desertnaut
Dec 28 '18 at 16:59





Question has nothing to do with machine-learning - kindly do not spam the tag (removed).

– desertnaut
Dec 28 '18 at 16:59













It was just to call in more help!

– Vaibhav Magon
Dec 29 '18 at 10:27





It was just to call in more help!

– Vaibhav Magon
Dec 29 '18 at 10:27













That's not a legitimate use of tags (let alone that the python/pandas/numpy community is already big enough) - kindly refrain from the practice next time

– desertnaut
Dec 29 '18 at 12:23





That's not a legitimate use of tags (let alone that the python/pandas/numpy community is already big enough) - kindly refrain from the practice next time

– desertnaut
Dec 29 '18 at 12:23












1 Answer
1






active

oldest

votes


















1














I think this should do the trick:



import pandas as pd
import numpy as np
from io import StringIO

TESTDATA = StringIO("""Ads,Impressions,Clicks
Ad_1, 11, 1
Ad_2, 10, 2
""")

table_1 = pd.read_csv(TESTDATA, sep=",")

def convert(row):
clicks_to_generate = row['Clicks']
array_len = row['Impressions']
ad = np.zeros(array_len)
ad[:clicks_to_generate] = 1
np.random.shuffle(ad) # you want it random
return ad

ads = table_1.apply(convert, axis=1)
series_list = [pd.Series(ad) for ad in ads]
table_2 = pd.DataFrame(series_list).T
table_2 = table_2.add_prefix('Ad_')
print(table_2)

Ad_0 Ad_1
0 0.0 0.0
1 1.0 0.0
2 0.0 1.0
3 0.0 1.0
4 0.0 0.0
5 0.0 0.0
6 0.0 0.0
7 0.0 0.0
8 0.0 0.0
9 0.0 0.0
10 0.0 NaN

table_2.to_csv('table_2.csv', index=False)





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%2f53959037%2fconverting-dataset-to-reward-miss-dataset-in-python%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









    1














    I think this should do the trick:



    import pandas as pd
    import numpy as np
    from io import StringIO

    TESTDATA = StringIO("""Ads,Impressions,Clicks
    Ad_1, 11, 1
    Ad_2, 10, 2
    """)

    table_1 = pd.read_csv(TESTDATA, sep=",")

    def convert(row):
    clicks_to_generate = row['Clicks']
    array_len = row['Impressions']
    ad = np.zeros(array_len)
    ad[:clicks_to_generate] = 1
    np.random.shuffle(ad) # you want it random
    return ad

    ads = table_1.apply(convert, axis=1)
    series_list = [pd.Series(ad) for ad in ads]
    table_2 = pd.DataFrame(series_list).T
    table_2 = table_2.add_prefix('Ad_')
    print(table_2)

    Ad_0 Ad_1
    0 0.0 0.0
    1 1.0 0.0
    2 0.0 1.0
    3 0.0 1.0
    4 0.0 0.0
    5 0.0 0.0
    6 0.0 0.0
    7 0.0 0.0
    8 0.0 0.0
    9 0.0 0.0
    10 0.0 NaN

    table_2.to_csv('table_2.csv', index=False)





    share|improve this answer






























      1














      I think this should do the trick:



      import pandas as pd
      import numpy as np
      from io import StringIO

      TESTDATA = StringIO("""Ads,Impressions,Clicks
      Ad_1, 11, 1
      Ad_2, 10, 2
      """)

      table_1 = pd.read_csv(TESTDATA, sep=",")

      def convert(row):
      clicks_to_generate = row['Clicks']
      array_len = row['Impressions']
      ad = np.zeros(array_len)
      ad[:clicks_to_generate] = 1
      np.random.shuffle(ad) # you want it random
      return ad

      ads = table_1.apply(convert, axis=1)
      series_list = [pd.Series(ad) for ad in ads]
      table_2 = pd.DataFrame(series_list).T
      table_2 = table_2.add_prefix('Ad_')
      print(table_2)

      Ad_0 Ad_1
      0 0.0 0.0
      1 1.0 0.0
      2 0.0 1.0
      3 0.0 1.0
      4 0.0 0.0
      5 0.0 0.0
      6 0.0 0.0
      7 0.0 0.0
      8 0.0 0.0
      9 0.0 0.0
      10 0.0 NaN

      table_2.to_csv('table_2.csv', index=False)





      share|improve this answer




























        1












        1








        1







        I think this should do the trick:



        import pandas as pd
        import numpy as np
        from io import StringIO

        TESTDATA = StringIO("""Ads,Impressions,Clicks
        Ad_1, 11, 1
        Ad_2, 10, 2
        """)

        table_1 = pd.read_csv(TESTDATA, sep=",")

        def convert(row):
        clicks_to_generate = row['Clicks']
        array_len = row['Impressions']
        ad = np.zeros(array_len)
        ad[:clicks_to_generate] = 1
        np.random.shuffle(ad) # you want it random
        return ad

        ads = table_1.apply(convert, axis=1)
        series_list = [pd.Series(ad) for ad in ads]
        table_2 = pd.DataFrame(series_list).T
        table_2 = table_2.add_prefix('Ad_')
        print(table_2)

        Ad_0 Ad_1
        0 0.0 0.0
        1 1.0 0.0
        2 0.0 1.0
        3 0.0 1.0
        4 0.0 0.0
        5 0.0 0.0
        6 0.0 0.0
        7 0.0 0.0
        8 0.0 0.0
        9 0.0 0.0
        10 0.0 NaN

        table_2.to_csv('table_2.csv', index=False)





        share|improve this answer















        I think this should do the trick:



        import pandas as pd
        import numpy as np
        from io import StringIO

        TESTDATA = StringIO("""Ads,Impressions,Clicks
        Ad_1, 11, 1
        Ad_2, 10, 2
        """)

        table_1 = pd.read_csv(TESTDATA, sep=",")

        def convert(row):
        clicks_to_generate = row['Clicks']
        array_len = row['Impressions']
        ad = np.zeros(array_len)
        ad[:clicks_to_generate] = 1
        np.random.shuffle(ad) # you want it random
        return ad

        ads = table_1.apply(convert, axis=1)
        series_list = [pd.Series(ad) for ad in ads]
        table_2 = pd.DataFrame(series_list).T
        table_2 = table_2.add_prefix('Ad_')
        print(table_2)

        Ad_0 Ad_1
        0 0.0 0.0
        1 1.0 0.0
        2 0.0 1.0
        3 0.0 1.0
        4 0.0 0.0
        5 0.0 0.0
        6 0.0 0.0
        7 0.0 0.0
        8 0.0 0.0
        9 0.0 0.0
        10 0.0 NaN

        table_2.to_csv('table_2.csv', index=False)






        share|improve this answer














        share|improve this answer



        share|improve this answer








        edited Dec 28 '18 at 15:35

























        answered Dec 28 '18 at 15:29









        Lukasz TracewskiLukasz Tracewski

        2,1211020




        2,1211020






























            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%2f53959037%2fconverting-dataset-to-reward-miss-dataset-in-python%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

            Angular Downloading a file using contenturl with Basic Authentication

            Olmecas

            Can't read property showImagePicker of undefined in react native iOS