How to pivot this using pandas?












1















I'm totally new to python/pandas so please bear with me.



I have been trying for what seems like an eternity to rearrange my dataframe (see the numbers beside in/out...). I have read several answers to what seem like similar questions on stack overflow but I can't get anything to work for me.



Initially I had a table that looked like this:intial table



I want to make it look like this (but with the actual dates and values):



company--date--date--date--date

ebay--------val-----val---val----val

amazon----val-----val---val----val


Initially I thought I could use df.pivot but then I didn't know what to put for index= because I didn't have a name for the columns. So I figured out how to give them a name using



df.columns.names = 'Company'


Now I have a table like this:table as it is now



Based on other answers I've seen on this site I tried again to use df.pivot 1.with index = 'Company' and I get KeyError: 'Company'
2.also with index = ['Company'] also get KeyError: 'Company'



Can anyone tell me what it is that I'm doing wrong?



Thanks in advance for your help










share|improve this question

























  • dont use links to pictures please.

    – Christian Sloper
    Nov 13 '18 at 17:31











  • It said I wasn't able to post pictures yet and have to use links. I assume this is because I'm new here.

    – Helen
    Nov 13 '18 at 17:33











  • stackoverflow.com/q/47152691/6361531

    – Scott Boston
    Nov 13 '18 at 17:33






  • 1





    You're passing 'Company' as the name of the column index, not the column name. In this case, your column names are already 'Ebay' and 'Amazon', which is why you get the "keyError". If you want to see or set the names of the columns, you can use print(df.columns) or df.columns = ['Col1','Col2']

    – G. Anderson
    Nov 13 '18 at 17:37











  • @G.Anderson I was trying to make the index of my pivoted dataframe the companies. I thought that by saying df.pivot(index='Company') I would achieve that. Am I totally misunderstanding something here?

    – Helen
    Nov 13 '18 at 17:58
















1















I'm totally new to python/pandas so please bear with me.



I have been trying for what seems like an eternity to rearrange my dataframe (see the numbers beside in/out...). I have read several answers to what seem like similar questions on stack overflow but I can't get anything to work for me.



Initially I had a table that looked like this:intial table



I want to make it look like this (but with the actual dates and values):



company--date--date--date--date

ebay--------val-----val---val----val

amazon----val-----val---val----val


Initially I thought I could use df.pivot but then I didn't know what to put for index= because I didn't have a name for the columns. So I figured out how to give them a name using



df.columns.names = 'Company'


Now I have a table like this:table as it is now



Based on other answers I've seen on this site I tried again to use df.pivot 1.with index = 'Company' and I get KeyError: 'Company'
2.also with index = ['Company'] also get KeyError: 'Company'



Can anyone tell me what it is that I'm doing wrong?



Thanks in advance for your help










share|improve this question

























  • dont use links to pictures please.

    – Christian Sloper
    Nov 13 '18 at 17:31











  • It said I wasn't able to post pictures yet and have to use links. I assume this is because I'm new here.

    – Helen
    Nov 13 '18 at 17:33











  • stackoverflow.com/q/47152691/6361531

    – Scott Boston
    Nov 13 '18 at 17:33






  • 1





    You're passing 'Company' as the name of the column index, not the column name. In this case, your column names are already 'Ebay' and 'Amazon', which is why you get the "keyError". If you want to see or set the names of the columns, you can use print(df.columns) or df.columns = ['Col1','Col2']

    – G. Anderson
    Nov 13 '18 at 17:37











  • @G.Anderson I was trying to make the index of my pivoted dataframe the companies. I thought that by saying df.pivot(index='Company') I would achieve that. Am I totally misunderstanding something here?

    – Helen
    Nov 13 '18 at 17:58














1












1








1








I'm totally new to python/pandas so please bear with me.



I have been trying for what seems like an eternity to rearrange my dataframe (see the numbers beside in/out...). I have read several answers to what seem like similar questions on stack overflow but I can't get anything to work for me.



Initially I had a table that looked like this:intial table



I want to make it look like this (but with the actual dates and values):



company--date--date--date--date

ebay--------val-----val---val----val

amazon----val-----val---val----val


Initially I thought I could use df.pivot but then I didn't know what to put for index= because I didn't have a name for the columns. So I figured out how to give them a name using



df.columns.names = 'Company'


Now I have a table like this:table as it is now



Based on other answers I've seen on this site I tried again to use df.pivot 1.with index = 'Company' and I get KeyError: 'Company'
2.also with index = ['Company'] also get KeyError: 'Company'



Can anyone tell me what it is that I'm doing wrong?



Thanks in advance for your help










share|improve this question
















I'm totally new to python/pandas so please bear with me.



I have been trying for what seems like an eternity to rearrange my dataframe (see the numbers beside in/out...). I have read several answers to what seem like similar questions on stack overflow but I can't get anything to work for me.



Initially I had a table that looked like this:intial table



I want to make it look like this (but with the actual dates and values):



company--date--date--date--date

ebay--------val-----val---val----val

amazon----val-----val---val----val


Initially I thought I could use df.pivot but then I didn't know what to put for index= because I didn't have a name for the columns. So I figured out how to give them a name using



df.columns.names = 'Company'


Now I have a table like this:table as it is now



Based on other answers I've seen on this site I tried again to use df.pivot 1.with index = 'Company' and I get KeyError: 'Company'
2.also with index = ['Company'] also get KeyError: 'Company'



Can anyone tell me what it is that I'm doing wrong?



Thanks in advance for your help







python pandas pivot






share|improve this question















share|improve this question













share|improve this question




share|improve this question








edited Nov 13 '18 at 17:32









pygo

2,4281619




2,4281619










asked Nov 13 '18 at 17:28









HelenHelen

84




84













  • dont use links to pictures please.

    – Christian Sloper
    Nov 13 '18 at 17:31











  • It said I wasn't able to post pictures yet and have to use links. I assume this is because I'm new here.

    – Helen
    Nov 13 '18 at 17:33











  • stackoverflow.com/q/47152691/6361531

    – Scott Boston
    Nov 13 '18 at 17:33






  • 1





    You're passing 'Company' as the name of the column index, not the column name. In this case, your column names are already 'Ebay' and 'Amazon', which is why you get the "keyError". If you want to see or set the names of the columns, you can use print(df.columns) or df.columns = ['Col1','Col2']

    – G. Anderson
    Nov 13 '18 at 17:37











  • @G.Anderson I was trying to make the index of my pivoted dataframe the companies. I thought that by saying df.pivot(index='Company') I would achieve that. Am I totally misunderstanding something here?

    – Helen
    Nov 13 '18 at 17:58



















  • dont use links to pictures please.

    – Christian Sloper
    Nov 13 '18 at 17:31











  • It said I wasn't able to post pictures yet and have to use links. I assume this is because I'm new here.

    – Helen
    Nov 13 '18 at 17:33











  • stackoverflow.com/q/47152691/6361531

    – Scott Boston
    Nov 13 '18 at 17:33






  • 1





    You're passing 'Company' as the name of the column index, not the column name. In this case, your column names are already 'Ebay' and 'Amazon', which is why you get the "keyError". If you want to see or set the names of the columns, you can use print(df.columns) or df.columns = ['Col1','Col2']

    – G. Anderson
    Nov 13 '18 at 17:37











  • @G.Anderson I was trying to make the index of my pivoted dataframe the companies. I thought that by saying df.pivot(index='Company') I would achieve that. Am I totally misunderstanding something here?

    – Helen
    Nov 13 '18 at 17:58

















dont use links to pictures please.

– Christian Sloper
Nov 13 '18 at 17:31





dont use links to pictures please.

– Christian Sloper
Nov 13 '18 at 17:31













It said I wasn't able to post pictures yet and have to use links. I assume this is because I'm new here.

– Helen
Nov 13 '18 at 17:33





It said I wasn't able to post pictures yet and have to use links. I assume this is because I'm new here.

– Helen
Nov 13 '18 at 17:33













stackoverflow.com/q/47152691/6361531

– Scott Boston
Nov 13 '18 at 17:33





stackoverflow.com/q/47152691/6361531

– Scott Boston
Nov 13 '18 at 17:33




1




1





You're passing 'Company' as the name of the column index, not the column name. In this case, your column names are already 'Ebay' and 'Amazon', which is why you get the "keyError". If you want to see or set the names of the columns, you can use print(df.columns) or df.columns = ['Col1','Col2']

– G. Anderson
Nov 13 '18 at 17:37





You're passing 'Company' as the name of the column index, not the column name. In this case, your column names are already 'Ebay' and 'Amazon', which is why you get the "keyError". If you want to see or set the names of the columns, you can use print(df.columns) or df.columns = ['Col1','Col2']

– G. Anderson
Nov 13 '18 at 17:37













@G.Anderson I was trying to make the index of my pivoted dataframe the companies. I thought that by saying df.pivot(index='Company') I would achieve that. Am I totally misunderstanding something here?

– Helen
Nov 13 '18 at 17:58





@G.Anderson I was trying to make the index of my pivoted dataframe the companies. I thought that by saying df.pivot(index='Company') I would achieve that. Am I totally misunderstanding something here?

– Helen
Nov 13 '18 at 17:58












1 Answer
1






active

oldest

votes


















1














Think you mean 'transpose' not 'pivot'. Can you please try doing the following?
df1.T. Full example below from pandas.DataFrame.transpose



setup:



d1 = {'col1': [1, 2], 'col2': [3, 4]}
df1 = pd.DataFrame(data=d1)
df1


output:



   col1  col2
0 1 3
1 2 4


code:



df1.T # or df1.transpose()


output:



      0  1
col1 1 2
col2 3 4





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%2f53286524%2fhow-to-pivot-this-using-pandas%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














    Think you mean 'transpose' not 'pivot'. Can you please try doing the following?
    df1.T. Full example below from pandas.DataFrame.transpose



    setup:



    d1 = {'col1': [1, 2], 'col2': [3, 4]}
    df1 = pd.DataFrame(data=d1)
    df1


    output:



       col1  col2
    0 1 3
    1 2 4


    code:



    df1.T # or df1.transpose()


    output:



          0  1
    col1 1 2
    col2 3 4





    share|improve this answer




























      1














      Think you mean 'transpose' not 'pivot'. Can you please try doing the following?
      df1.T. Full example below from pandas.DataFrame.transpose



      setup:



      d1 = {'col1': [1, 2], 'col2': [3, 4]}
      df1 = pd.DataFrame(data=d1)
      df1


      output:



         col1  col2
      0 1 3
      1 2 4


      code:



      df1.T # or df1.transpose()


      output:



            0  1
      col1 1 2
      col2 3 4





      share|improve this answer


























        1












        1








        1







        Think you mean 'transpose' not 'pivot'. Can you please try doing the following?
        df1.T. Full example below from pandas.DataFrame.transpose



        setup:



        d1 = {'col1': [1, 2], 'col2': [3, 4]}
        df1 = pd.DataFrame(data=d1)
        df1


        output:



           col1  col2
        0 1 3
        1 2 4


        code:



        df1.T # or df1.transpose()


        output:



              0  1
        col1 1 2
        col2 3 4





        share|improve this answer













        Think you mean 'transpose' not 'pivot'. Can you please try doing the following?
        df1.T. Full example below from pandas.DataFrame.transpose



        setup:



        d1 = {'col1': [1, 2], 'col2': [3, 4]}
        df1 = pd.DataFrame(data=d1)
        df1


        output:



           col1  col2
        0 1 3
        1 2 4


        code:



        df1.T # or df1.transpose()


        output:



              0  1
        col1 1 2
        col2 3 4






        share|improve this answer












        share|improve this answer



        share|improve this answer










        answered Nov 13 '18 at 17:37









        jeevsjeevs

        1165




        1165






























            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%2f53286524%2fhow-to-pivot-this-using-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







            這個網誌中的熱門文章

            Academy of Television Arts & Sciences

            L'Équipe

            1995 France bombings