Selecting random rows (of data) from dataframe / csv file in Pyhton after designating start and end row...











up vote
0
down vote

favorite












Using the sample() function I can get the random rows. Data set having 1000000 rows of data and I want to have a subset of 20000 rows. Importing random lines can be done through this solution



https://stackoverflow.com/a/22259008/8966221



reading a dataset



dataset = read_csv(file_path)


dataset_sub = dataset.sample(20000, random_state=1)



However I want to select random rows between row number 250000 to 750000. Any possible solution in that regard?.










share|improve this question




























    up vote
    0
    down vote

    favorite












    Using the sample() function I can get the random rows. Data set having 1000000 rows of data and I want to have a subset of 20000 rows. Importing random lines can be done through this solution



    https://stackoverflow.com/a/22259008/8966221



    reading a dataset



    dataset = read_csv(file_path)


    dataset_sub = dataset.sample(20000, random_state=1)



    However I want to select random rows between row number 250000 to 750000. Any possible solution in that regard?.










    share|improve this question


























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      Using the sample() function I can get the random rows. Data set having 1000000 rows of data and I want to have a subset of 20000 rows. Importing random lines can be done through this solution



      https://stackoverflow.com/a/22259008/8966221



      reading a dataset



      dataset = read_csv(file_path)


      dataset_sub = dataset.sample(20000, random_state=1)



      However I want to select random rows between row number 250000 to 750000. Any possible solution in that regard?.










      share|improve this question















      Using the sample() function I can get the random rows. Data set having 1000000 rows of data and I want to have a subset of 20000 rows. Importing random lines can be done through this solution



      https://stackoverflow.com/a/22259008/8966221



      reading a dataset



      dataset = read_csv(file_path)


      dataset_sub = dataset.sample(20000, random_state=1)



      However I want to select random rows between row number 250000 to 750000. Any possible solution in that regard?.







      python random rows






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 19 at 7:25









      Md. Mokammal Hossen Farnan

      585320




      585320










      asked Nov 19 at 6:55









      Devarshi Mandal

      14




      14
























          3 Answers
          3






          active

          oldest

          votes

















          up vote
          0
          down vote













          I think you need this:



          dataset = read_csv(file_path)
          dataset_sub = dataset.sample(random.randint(250000,750000), random_state=1)





          share|improve this answer





















          • Thanks for your reply, but I want to randomly extract only say 20,000 rows. I think that argument is also to be entered
            – Devarshi Mandal
            Nov 19 at 10:14


















          up vote
          0
          down vote













          What you can do is to create a DataFrame containing the rows between row number 250000 to 750000, then select 20000 random rows from that.



          dataset_sub = dataset.loc[250000:750000].sample(20000, random_state=1)





          share|improve this answer





















          • Thanks this is helpful
            – Devarshi Mandal
            Nov 19 at 10:16


















          up vote
          0
          down vote













          I think the following code works:



          import random
          a=random.sample(range(250000,750000), 20000)
          data=dataset.loc[a]





          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',
            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%2f53369669%2fselecting-random-rows-of-data-from-dataframe-csv-file-in-pyhton-after-design%23new-answer', 'question_page');
            }
            );

            Post as a guest















            Required, but never shown

























            3 Answers
            3






            active

            oldest

            votes








            3 Answers
            3






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote













            I think you need this:



            dataset = read_csv(file_path)
            dataset_sub = dataset.sample(random.randint(250000,750000), random_state=1)





            share|improve this answer





















            • Thanks for your reply, but I want to randomly extract only say 20,000 rows. I think that argument is also to be entered
              – Devarshi Mandal
              Nov 19 at 10:14















            up vote
            0
            down vote













            I think you need this:



            dataset = read_csv(file_path)
            dataset_sub = dataset.sample(random.randint(250000,750000), random_state=1)





            share|improve this answer





















            • Thanks for your reply, but I want to randomly extract only say 20,000 rows. I think that argument is also to be entered
              – Devarshi Mandal
              Nov 19 at 10:14













            up vote
            0
            down vote










            up vote
            0
            down vote









            I think you need this:



            dataset = read_csv(file_path)
            dataset_sub = dataset.sample(random.randint(250000,750000), random_state=1)





            share|improve this answer












            I think you need this:



            dataset = read_csv(file_path)
            dataset_sub = dataset.sample(random.randint(250000,750000), random_state=1)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 at 7:08









            Rudolf Morkovskyi

            714116




            714116












            • Thanks for your reply, but I want to randomly extract only say 20,000 rows. I think that argument is also to be entered
              – Devarshi Mandal
              Nov 19 at 10:14


















            • Thanks for your reply, but I want to randomly extract only say 20,000 rows. I think that argument is also to be entered
              – Devarshi Mandal
              Nov 19 at 10:14
















            Thanks for your reply, but I want to randomly extract only say 20,000 rows. I think that argument is also to be entered
            – Devarshi Mandal
            Nov 19 at 10:14




            Thanks for your reply, but I want to randomly extract only say 20,000 rows. I think that argument is also to be entered
            – Devarshi Mandal
            Nov 19 at 10:14












            up vote
            0
            down vote













            What you can do is to create a DataFrame containing the rows between row number 250000 to 750000, then select 20000 random rows from that.



            dataset_sub = dataset.loc[250000:750000].sample(20000, random_state=1)





            share|improve this answer





















            • Thanks this is helpful
              – Devarshi Mandal
              Nov 19 at 10:16















            up vote
            0
            down vote













            What you can do is to create a DataFrame containing the rows between row number 250000 to 750000, then select 20000 random rows from that.



            dataset_sub = dataset.loc[250000:750000].sample(20000, random_state=1)





            share|improve this answer





















            • Thanks this is helpful
              – Devarshi Mandal
              Nov 19 at 10:16













            up vote
            0
            down vote










            up vote
            0
            down vote









            What you can do is to create a DataFrame containing the rows between row number 250000 to 750000, then select 20000 random rows from that.



            dataset_sub = dataset.loc[250000:750000].sample(20000, random_state=1)





            share|improve this answer












            What you can do is to create a DataFrame containing the rows between row number 250000 to 750000, then select 20000 random rows from that.



            dataset_sub = dataset.loc[250000:750000].sample(20000, random_state=1)






            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Nov 19 at 7:10









            Andreas

            1,7761618




            1,7761618












            • Thanks this is helpful
              – Devarshi Mandal
              Nov 19 at 10:16


















            • Thanks this is helpful
              – Devarshi Mandal
              Nov 19 at 10:16
















            Thanks this is helpful
            – Devarshi Mandal
            Nov 19 at 10:16




            Thanks this is helpful
            – Devarshi Mandal
            Nov 19 at 10:16










            up vote
            0
            down vote













            I think the following code works:



            import random
            a=random.sample(range(250000,750000), 20000)
            data=dataset.loc[a]





            share|improve this answer

























              up vote
              0
              down vote













              I think the following code works:



              import random
              a=random.sample(range(250000,750000), 20000)
              data=dataset.loc[a]





              share|improve this answer























                up vote
                0
                down vote










                up vote
                0
                down vote









                I think the following code works:



                import random
                a=random.sample(range(250000,750000), 20000)
                data=dataset.loc[a]





                share|improve this answer












                I think the following code works:



                import random
                a=random.sample(range(250000,750000), 20000)
                data=dataset.loc[a]






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 22 at 15:21









                Enayat Rajabi

                9731129




                9731129






























                    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.





                    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.




                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function () {
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53369669%2fselecting-random-rows-of-data-from-dataframe-csv-file-in-pyhton-after-design%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

                    What visual should I use to simply compare current year value vs last year in Power BI desktop

                    Alexandru Averescu

                    Trompette piccolo