Pandas - Performing a transpose of multiple columns





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







1















I have a Dataframe as below:



col1,col2,col3,col4
value1,value2,value3,value4


I am trying to perform a transpose function on them to get the below output:



col1,value1
col2,value2
col3,value3
col4,value4


The problem is the list of columns are dynamic and it depends on user input that pulls the respective columns at the first place.



Use the below query to get the list of columns the user has requested:



cursor.execute('select {} from table'.format(', '.join(str(cols) for cols in col_names)))


Could anyone advice how I could perform a transpose of such a dataframe that has multiple columns but just one row of output. Thanks..










share|improve this question


















  • 3





    Have you tried using df.T ?

    – dataLeo
    Nov 25 '18 at 5:40











  • @dataLeo thanks that helped..

    – dark horse
    Nov 25 '18 at 18:07


















1















I have a Dataframe as below:



col1,col2,col3,col4
value1,value2,value3,value4


I am trying to perform a transpose function on them to get the below output:



col1,value1
col2,value2
col3,value3
col4,value4


The problem is the list of columns are dynamic and it depends on user input that pulls the respective columns at the first place.



Use the below query to get the list of columns the user has requested:



cursor.execute('select {} from table'.format(', '.join(str(cols) for cols in col_names)))


Could anyone advice how I could perform a transpose of such a dataframe that has multiple columns but just one row of output. Thanks..










share|improve this question


















  • 3





    Have you tried using df.T ?

    – dataLeo
    Nov 25 '18 at 5:40











  • @dataLeo thanks that helped..

    – dark horse
    Nov 25 '18 at 18:07














1












1








1








I have a Dataframe as below:



col1,col2,col3,col4
value1,value2,value3,value4


I am trying to perform a transpose function on them to get the below output:



col1,value1
col2,value2
col3,value3
col4,value4


The problem is the list of columns are dynamic and it depends on user input that pulls the respective columns at the first place.



Use the below query to get the list of columns the user has requested:



cursor.execute('select {} from table'.format(', '.join(str(cols) for cols in col_names)))


Could anyone advice how I could perform a transpose of such a dataframe that has multiple columns but just one row of output. Thanks..










share|improve this question














I have a Dataframe as below:



col1,col2,col3,col4
value1,value2,value3,value4


I am trying to perform a transpose function on them to get the below output:



col1,value1
col2,value2
col3,value3
col4,value4


The problem is the list of columns are dynamic and it depends on user input that pulls the respective columns at the first place.



Use the below query to get the list of columns the user has requested:



cursor.execute('select {} from table'.format(', '.join(str(cols) for cols in col_names)))


Could anyone advice how I could perform a transpose of such a dataframe that has multiple columns but just one row of output. Thanks..







python python-3.x pandas pandas-groupby






share|improve this question













share|improve this question











share|improve this question




share|improve this question










asked Nov 25 '18 at 5:12









dark horsedark horse

16210




16210








  • 3





    Have you tried using df.T ?

    – dataLeo
    Nov 25 '18 at 5:40











  • @dataLeo thanks that helped..

    – dark horse
    Nov 25 '18 at 18:07














  • 3





    Have you tried using df.T ?

    – dataLeo
    Nov 25 '18 at 5:40











  • @dataLeo thanks that helped..

    – dark horse
    Nov 25 '18 at 18:07








3




3





Have you tried using df.T ?

– dataLeo
Nov 25 '18 at 5:40





Have you tried using df.T ?

– dataLeo
Nov 25 '18 at 5:40













@dataLeo thanks that helped..

– dark horse
Nov 25 '18 at 18:07





@dataLeo thanks that helped..

– dark horse
Nov 25 '18 at 18:07












1 Answer
1






active

oldest

votes


















1














If your dataframe construct is the way you have shown in your post with the multiple columns and single row then simply set the index to your first column, in your case its "col1" and then transpose it.



DataFrame as per shown:



df = pd.DataFrame({ 'col1':['value1'], 'col2':['value2'], 'col3':['value2'], 'col4':['value4']})

col1 col2 col3 col4
0 value1 value2 value2 value4


You can perform like below:



>>> df.set_index('col1',inplace=True) # First set the Index to `col1` + `inplace=True`


Now allow transpose ..



>>> df.T
col1 value1
col2 value2
col3 value2
col4 value4


In one go all :



>>> df.set_index('col1').T
col1 value1
col2 value2
col3 value2
col4 value4


Note: df.T will not do the Job because it seeks the default index ie zero in this case to be as columns.



Hope this will help you going.






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%2f53464845%2fpandas-performing-a-transpose-of-multiple-columns%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














    If your dataframe construct is the way you have shown in your post with the multiple columns and single row then simply set the index to your first column, in your case its "col1" and then transpose it.



    DataFrame as per shown:



    df = pd.DataFrame({ 'col1':['value1'], 'col2':['value2'], 'col3':['value2'], 'col4':['value4']})

    col1 col2 col3 col4
    0 value1 value2 value2 value4


    You can perform like below:



    >>> df.set_index('col1',inplace=True) # First set the Index to `col1` + `inplace=True`


    Now allow transpose ..



    >>> df.T
    col1 value1
    col2 value2
    col3 value2
    col4 value4


    In one go all :



    >>> df.set_index('col1').T
    col1 value1
    col2 value2
    col3 value2
    col4 value4


    Note: df.T will not do the Job because it seeks the default index ie zero in this case to be as columns.



    Hope this will help you going.






    share|improve this answer




























      1














      If your dataframe construct is the way you have shown in your post with the multiple columns and single row then simply set the index to your first column, in your case its "col1" and then transpose it.



      DataFrame as per shown:



      df = pd.DataFrame({ 'col1':['value1'], 'col2':['value2'], 'col3':['value2'], 'col4':['value4']})

      col1 col2 col3 col4
      0 value1 value2 value2 value4


      You can perform like below:



      >>> df.set_index('col1',inplace=True) # First set the Index to `col1` + `inplace=True`


      Now allow transpose ..



      >>> df.T
      col1 value1
      col2 value2
      col3 value2
      col4 value4


      In one go all :



      >>> df.set_index('col1').T
      col1 value1
      col2 value2
      col3 value2
      col4 value4


      Note: df.T will not do the Job because it seeks the default index ie zero in this case to be as columns.



      Hope this will help you going.






      share|improve this answer


























        1












        1








        1







        If your dataframe construct is the way you have shown in your post with the multiple columns and single row then simply set the index to your first column, in your case its "col1" and then transpose it.



        DataFrame as per shown:



        df = pd.DataFrame({ 'col1':['value1'], 'col2':['value2'], 'col3':['value2'], 'col4':['value4']})

        col1 col2 col3 col4
        0 value1 value2 value2 value4


        You can perform like below:



        >>> df.set_index('col1',inplace=True) # First set the Index to `col1` + `inplace=True`


        Now allow transpose ..



        >>> df.T
        col1 value1
        col2 value2
        col3 value2
        col4 value4


        In one go all :



        >>> df.set_index('col1').T
        col1 value1
        col2 value2
        col3 value2
        col4 value4


        Note: df.T will not do the Job because it seeks the default index ie zero in this case to be as columns.



        Hope this will help you going.






        share|improve this answer













        If your dataframe construct is the way you have shown in your post with the multiple columns and single row then simply set the index to your first column, in your case its "col1" and then transpose it.



        DataFrame as per shown:



        df = pd.DataFrame({ 'col1':['value1'], 'col2':['value2'], 'col3':['value2'], 'col4':['value4']})

        col1 col2 col3 col4
        0 value1 value2 value2 value4


        You can perform like below:



        >>> df.set_index('col1',inplace=True) # First set the Index to `col1` + `inplace=True`


        Now allow transpose ..



        >>> df.T
        col1 value1
        col2 value2
        col3 value2
        col4 value4


        In one go all :



        >>> df.set_index('col1').T
        col1 value1
        col2 value2
        col3 value2
        col4 value4


        Note: df.T will not do the Job because it seeks the default index ie zero in this case to be as columns.



        Hope this will help you going.







        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 25 '18 at 16:26









        pygopygo

        3,2451721




        3,2451721
































            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%2f53464845%2fpandas-performing-a-transpose-of-multiple-columns%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







            這個網誌中的熱門文章

            Post-Redirect-Get with Spring WebFlux and Thymeleaf

            Xamarin.form Move up view when keyboard appear

            JBPM : POST request for execute process go wrong