How to select random column from dataframe in pandas?What does the “yield” keyword do?How to randomly select an item from a list?How do I sort a dictionary by value?Selecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers

Why are non-collision-resistant hash functions considered insecure for signing self-generated information

Why do banks “park” their money at the European Central Bank?

Did the British navy fail to take into account the ballistics correction due to Coriolis force during WW1 Falkland Islands battle?

Antonym of "billable"

Did a flight controller ever answer Flight with a no-go?

If all stars rotate, why was there a theory developed that requires non-rotating stars?

What is this symbol: semicircles facing each other?

Would the Republic of Ireland and Northern Ireland be interested in reuniting?

Would it be possible to have a GMO that produces chocolate?

What would be the challenges to taking off and landing a typical passenger jet at FL300?

Position a tabular on the corner of a slide

How to find out the average duration of the peer-review process for a given journal?

Is a player able to change alignment midway through an adventure?

Is "The life is beautiful" incorrect or just very non-idiomatic?

How do we calculate energy of food?

Is there any example of one country devastating a third?

Was it ever possible to target a zone?

How much authority do teachers get from *In Loco Parentis*?

How do I get toddlers to stop asking for food every hour?

Did anyone try to find the little box that held Professor Moriarty and his wife after the Enterprise D crashed?

Why would an IIS hosted site prompt for AD account credential if accessed through a hostname or IP, but not through servername?

Understanding Parallelize methods

Can a Rogue PC teach an NPC to perform Sneak Attack?

Disambiguation of "nobis vobis" and "nobis nobis"



How to select random column from dataframe in pandas?


What does the “yield” keyword do?How to randomly select an item from a list?How do I sort a dictionary by value?Selecting multiple columns in a pandas dataframeRenaming columns in pandasAdding new column to existing DataFrame in Python pandasDelete column from pandas DataFrameHow to iterate over rows in a DataFrame in Pandas?Select rows from a DataFrame based on values in a column in pandasGet list from pandas DataFrame column headers






.everyoneloves__top-leaderboard:empty,.everyoneloves__mid-leaderboard:empty,.everyoneloves__bot-mid-leaderboard:empty margin-bottom:0;








-1















I want to select random value from data frame.



For example:



if df=[a,b,c,d] I want to select random from these 4 options










share|improve this question





















  • 2





    df.samplepandas.pydata.org/pandas-docs/stable/reference/api/…

    – WeNYoBen
    Mar 27 at 17:32

















-1















I want to select random value from data frame.



For example:



if df=[a,b,c,d] I want to select random from these 4 options










share|improve this question





















  • 2





    df.samplepandas.pydata.org/pandas-docs/stable/reference/api/…

    – WeNYoBen
    Mar 27 at 17:32













-1












-1








-1








I want to select random value from data frame.



For example:



if df=[a,b,c,d] I want to select random from these 4 options










share|improve this question
















I want to select random value from data frame.



For example:



if df=[a,b,c,d] I want to select random from these 4 options







python pandas






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Mar 31 at 7:06









marc_s

602k136 gold badges1150 silver badges1288 bronze badges




602k136 gold badges1150 silver badges1288 bronze badges










asked Mar 27 at 17:31









john terryjohn terry

426 bronze badges




426 bronze badges










  • 2





    df.samplepandas.pydata.org/pandas-docs/stable/reference/api/…

    – WeNYoBen
    Mar 27 at 17:32












  • 2





    df.samplepandas.pydata.org/pandas-docs/stable/reference/api/…

    – WeNYoBen
    Mar 27 at 17:32







2




2





df.samplepandas.pydata.org/pandas-docs/stable/reference/api/…

– WeNYoBen
Mar 27 at 17:32





df.samplepandas.pydata.org/pandas-docs/stable/reference/api/…

– WeNYoBen
Mar 27 at 17:32












2 Answers
2






active

oldest

votes


















0















Your comments suggest you want to return a random column rather than random row. You can still achieve this using df.sample(). You just have to specify the axis.



import pandas as pd

d = (
'A' : [1,2,3,4],
'B' : [1,2,3,4],
'C' : [1,2,3,4],
'D' : [1,2,3,4],
)

df = pd.DataFrame(data = d)

#Return random Column
df_Col = df.sample(axis=1)

#Return random Row
df_Row = df.sample()





share|improve this answer
































    0















    If you want to select certain column you can to something similar to indexing in numpy .



    df_new = df[2]
    df_new = c
    print c[row_number]


    First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.






    share|improve this answer

























    • Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.

      – john terry
      Mar 27 at 18:01











    • May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.

      – Astro
      Mar 27 at 18:43














    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%2f55383314%2fhow-to-select-random-column-from-dataframe-in-pandas%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









    0















    Your comments suggest you want to return a random column rather than random row. You can still achieve this using df.sample(). You just have to specify the axis.



    import pandas as pd

    d = (
    'A' : [1,2,3,4],
    'B' : [1,2,3,4],
    'C' : [1,2,3,4],
    'D' : [1,2,3,4],
    )

    df = pd.DataFrame(data = d)

    #Return random Column
    df_Col = df.sample(axis=1)

    #Return random Row
    df_Row = df.sample()





    share|improve this answer





























      0















      Your comments suggest you want to return a random column rather than random row. You can still achieve this using df.sample(). You just have to specify the axis.



      import pandas as pd

      d = (
      'A' : [1,2,3,4],
      'B' : [1,2,3,4],
      'C' : [1,2,3,4],
      'D' : [1,2,3,4],
      )

      df = pd.DataFrame(data = d)

      #Return random Column
      df_Col = df.sample(axis=1)

      #Return random Row
      df_Row = df.sample()





      share|improve this answer



























        0














        0










        0









        Your comments suggest you want to return a random column rather than random row. You can still achieve this using df.sample(). You just have to specify the axis.



        import pandas as pd

        d = (
        'A' : [1,2,3,4],
        'B' : [1,2,3,4],
        'C' : [1,2,3,4],
        'D' : [1,2,3,4],
        )

        df = pd.DataFrame(data = d)

        #Return random Column
        df_Col = df.sample(axis=1)

        #Return random Row
        df_Row = df.sample()





        share|improve this answer













        Your comments suggest you want to return a random column rather than random row. You can still achieve this using df.sample(). You just have to specify the axis.



        import pandas as pd

        d = (
        'A' : [1,2,3,4],
        'B' : [1,2,3,4],
        'C' : [1,2,3,4],
        'D' : [1,2,3,4],
        )

        df = pd.DataFrame(data = d)

        #Return random Column
        df_Col = df.sample(axis=1)

        #Return random Row
        df_Row = df.sample()






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Mar 27 at 22:25









        jonboyjonboy

        622 silver badges15 bronze badges




        622 silver badges15 bronze badges


























            0















            If you want to select certain column you can to something similar to indexing in numpy .



            df_new = df[2]
            df_new = c
            print c[row_number]


            First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.






            share|improve this answer

























            • Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.

              – john terry
              Mar 27 at 18:01











            • May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.

              – Astro
              Mar 27 at 18:43
















            0















            If you want to select certain column you can to something similar to indexing in numpy .



            df_new = df[2]
            df_new = c
            print c[row_number]


            First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.






            share|improve this answer

























            • Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.

              – john terry
              Mar 27 at 18:01











            • May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.

              – Astro
              Mar 27 at 18:43














            0














            0










            0









            If you want to select certain column you can to something similar to indexing in numpy .



            df_new = df[2]
            df_new = c
            print c[row_number]


            First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.






            share|improve this answer













            If you want to select certain column you can to something similar to indexing in numpy .



            df_new = df[2]
            df_new = c
            print c[row_number]


            First you select column you want the data value from . Let us say we want the value from column c so we index second column and put it into new dataframe .The call this new dataframe c . Now you have the column that you want your data from .Just type the row number in the code and you will get your desire value printed.







            share|improve this answer












            share|improve this answer



            share|improve this answer










            answered Mar 27 at 17:44









            AstroAstro

            136 bronze badges




            136 bronze badges















            • Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.

              – john terry
              Mar 27 at 18:01











            • May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.

              – Astro
              Mar 27 at 18:43


















            • Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.

              – john terry
              Mar 27 at 18:01











            • May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.

              – Astro
              Mar 27 at 18:43

















            Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.

            – john terry
            Mar 27 at 18:01





            Actually I want to select random value from data not from particular column but random column. If I have [100,20,30,15] I want to select randomly from these values.

            – john terry
            Mar 27 at 18:01













            May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.

            – Astro
            Mar 27 at 18:43






            May be you are asking for df.sample() that will give you some random row from your data . You can change value of n to get as many random values you want for example df.sample(n = 3) will give you 3 random values from your data.

            – Astro
            Mar 27 at 18:43


















            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%2f55383314%2fhow-to-select-random-column-from-dataframe-in-pandas%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

            Kamusi Yaliyomo Aina za kamusi | Muundo wa kamusi | Faida za kamusi | Dhima ya picha katika kamusi | Marejeo | Tazama pia | Viungo vya nje | UrambazajiKuhusu kamusiGo-SwahiliWiki-KamusiKamusi ya Kiswahili na Kiingerezakuihariri na kuongeza habari

            Swift 4 - func physicsWorld not invoked on collision? The Next CEO of Stack OverflowHow to call Objective-C code from Swift#ifdef replacement in the Swift language@selector() in Swift?#pragma mark in Swift?Swift for loop: for index, element in array?dispatch_after - GCD in Swift?Swift Beta performance: sorting arraysSplit a String into an array in Swift?The use of Swift 3 @objc inference in Swift 4 mode is deprecated?How to optimize UITableViewCell, because my UITableView lags

            Access current req object everywhere in Node.js ExpressWhy are global variables considered bad practice? (node.js)Using req & res across functionsHow do I get the path to the current script with Node.js?What is Node.js' Connect, Express and “middleware”?Node.js w/ express error handling in callbackHow to access the GET parameters after “?” in Express?Modify Node.js req object parametersAccess “app” variable inside of ExpressJS/ConnectJS middleware?Node.js Express app - request objectAngular Http Module considered middleware?Session variables in ExpressJSAdd properties to the req object in expressjs with Typescript