I want to append the rows of a dataframe as columnsWhat is the difference between Python's list methods append and extend?How to sort a dataframe by multiple column(s)Renaming columns in pandasFilter dataframe rows if value in column is in a set list of valuesAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeDelete 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

Reflecting Telescope Blind Spot?

Bullying by school - Submitted PhD thesis but not allowed to proceed to viva until change to new supervisor

Idiom for 'person who gets violent when drunk"

Threading data on TimeSeries

Print the phrase "And she said, 'But that's his.'" using only the alphabet

How can I detect if I'm in a subshell?

How to remove multiple elements from Set/Map AND knowing which ones were removed?

Can an open source licence be revoked if it violates employer's IP?

Why is gun control associated with the socially liberal Democratic party?

What is the difference between state-based effects and effects on the stack?

Are athletes' college degrees discounted by employers and graduate school admissions?

Why not make one big CPU core?

Manager wants to hire me; HR does not. How to proceed?

Arcane Tradition and Cost Efficiency: Learn spells on level-up, or learn them from scrolls/spellbooks?

Sakkāya-Ditthi and Self-View

What things do I only get a limited opportunity to take photos of?

Should I worry about having my credit pulled multiple times while car shopping?

How to avoid offending original culture when making conculture inspired from original

Nth term of Van Eck Sequence

What is the color associated with lukewarm?

mathrm in LaTeX

Does an African-American baby born in Youngstown, Ohio have a higher infant mortality rate than a baby born in Iran?

The title "Mord mit Aussicht" explained

Do items with curse of vanishing disappear from shulker boxes?



I want to append the rows of a dataframe as columns


What is the difference between Python's list methods append and extend?How to sort a dataframe by multiple column(s)Renaming columns in pandasFilter dataframe rows if value in column is in a set list of valuesAdding new column to existing DataFrame in Python pandasHow can I replace all the NaN values with Zero's in a column of a pandas dataframeDelete 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 height:90px;width:728px;box-sizing:border-box;








2















I have several small dataframes such as:



name x y z
A 1 2 3
A 1 23 4
A 3 5 6
B 0 2 3


And I want to append all the "A"s such that I get this dataframe



name x y z x2 y2 z2 x3 y3 z3
A 1 2 3 1 23 4 3 5 6
B 0 2 3 NaN-------------------> NaN


Any help would be appreciated, sorry if the above tables aren't spaced out properly










share|improve this question






























    2















    I have several small dataframes such as:



    name x y z
    A 1 2 3
    A 1 23 4
    A 3 5 6
    B 0 2 3


    And I want to append all the "A"s such that I get this dataframe



    name x y z x2 y2 z2 x3 y3 z3
    A 1 2 3 1 23 4 3 5 6
    B 0 2 3 NaN-------------------> NaN


    Any help would be appreciated, sorry if the above tables aren't spaced out properly










    share|improve this question


























      2












      2








      2








      I have several small dataframes such as:



      name x y z
      A 1 2 3
      A 1 23 4
      A 3 5 6
      B 0 2 3


      And I want to append all the "A"s such that I get this dataframe



      name x y z x2 y2 z2 x3 y3 z3
      A 1 2 3 1 23 4 3 5 6
      B 0 2 3 NaN-------------------> NaN


      Any help would be appreciated, sorry if the above tables aren't spaced out properly










      share|improve this question
















      I have several small dataframes such as:



      name x y z
      A 1 2 3
      A 1 23 4
      A 3 5 6
      B 0 2 3


      And I want to append all the "A"s such that I get this dataframe



      name x y z x2 y2 z2 x3 y3 z3
      A 1 2 3 1 23 4 3 5 6
      B 0 2 3 NaN-------------------> NaN


      Any help would be appreciated, sorry if the above tables aren't spaced out properly







      python pandas dataframe rows col






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Mar 25 at 2:50









      U9-Forward

      21.7k51847




      21.7k51847










      asked Mar 25 at 2:46









      Ashish MistryAshish Mistry

      163




      163






















          2 Answers
          2






          active

          oldest

          votes


















          2














          More like a pivot problem after create the key by cumcount--- I am using unstack here



          df['Newkey']=df.groupby('name').cumcount()+1
          yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
          yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
          yourdf
          Out[20]:
          x1 y1 z1 x2 y2 z2 x3 y3 z3
          name
          A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
          B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN





          share|improve this answer






























            2














            Use pivot_table with some couple of other functions:



            df['idx'] = df.groupby('name').cumcount()+1
            df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
            df = df.sort_index(axis=1, level=1)
            df.columns = [f'x_y' for x,y in df.columns]
            df = df.reset_index()


            And now:



            print(df)


            Reproduces:



             name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
            0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
            1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN





            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%2f55330648%2fi-want-to-append-the-rows-of-a-dataframe-as-columns%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









              2














              More like a pivot problem after create the key by cumcount--- I am using unstack here



              df['Newkey']=df.groupby('name').cumcount()+1
              yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
              yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
              yourdf
              Out[20]:
              x1 y1 z1 x2 y2 z2 x3 y3 z3
              name
              A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
              B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN





              share|improve this answer



























                2














                More like a pivot problem after create the key by cumcount--- I am using unstack here



                df['Newkey']=df.groupby('name').cumcount()+1
                yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
                yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
                yourdf
                Out[20]:
                x1 y1 z1 x2 y2 z2 x3 y3 z3
                name
                A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
                B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN





                share|improve this answer

























                  2












                  2








                  2







                  More like a pivot problem after create the key by cumcount--- I am using unstack here



                  df['Newkey']=df.groupby('name').cumcount()+1
                  yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
                  yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
                  yourdf
                  Out[20]:
                  x1 y1 z1 x2 y2 z2 x3 y3 z3
                  name
                  A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
                  B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN





                  share|improve this answer













                  More like a pivot problem after create the key by cumcount--- I am using unstack here



                  df['Newkey']=df.groupby('name').cumcount()+1
                  yourdf=df.set_index(['name','Newkey']).unstack().sort_index(level=1,axis=1)
                  yourdf.columns=yourdf.columns.map('0[0]0[1]'.format)
                  yourdf
                  Out[20]:
                  x1 y1 z1 x2 y2 z2 x3 y3 z3
                  name
                  A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
                  B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN






                  share|improve this answer












                  share|improve this answer



                  share|improve this answer










                  answered Mar 25 at 2:51









                  WeNYoBenWeNYoBen

                  140k84978




                  140k84978























                      2














                      Use pivot_table with some couple of other functions:



                      df['idx'] = df.groupby('name').cumcount()+1
                      df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
                      df = df.sort_index(axis=1, level=1)
                      df.columns = [f'x_y' for x,y in df.columns]
                      df = df.reset_index()


                      And now:



                      print(df)


                      Reproduces:



                       name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
                      0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
                      1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN





                      share|improve this answer



























                        2














                        Use pivot_table with some couple of other functions:



                        df['idx'] = df.groupby('name').cumcount()+1
                        df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
                        df = df.sort_index(axis=1, level=1)
                        df.columns = [f'x_y' for x,y in df.columns]
                        df = df.reset_index()


                        And now:



                        print(df)


                        Reproduces:



                         name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
                        0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
                        1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN





                        share|improve this answer

























                          2












                          2








                          2







                          Use pivot_table with some couple of other functions:



                          df['idx'] = df.groupby('name').cumcount()+1
                          df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
                          df = df.sort_index(axis=1, level=1)
                          df.columns = [f'x_y' for x,y in df.columns]
                          df = df.reset_index()


                          And now:



                          print(df)


                          Reproduces:



                           name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
                          0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
                          1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN





                          share|improve this answer













                          Use pivot_table with some couple of other functions:



                          df['idx'] = df.groupby('name').cumcount()+1
                          df = df.pivot_table(index='name', columns='idx', values=['x', 'y', 'z'], aggfunc='first')
                          df = df.sort_index(axis=1, level=1)
                          df.columns = [f'x_y' for x,y in df.columns]
                          df = df.reset_index()


                          And now:



                          print(df)


                          Reproduces:



                           name x_1 y_1 z_1 x_2 y_2 z_2 x_3 y_3 z_3
                          0 A 1.0 2.0 3.0 1.0 23.0 4.0 3.0 5.0 6.0
                          1 B 0.0 2.0 3.0 NaN NaN NaN NaN NaN NaN






                          share|improve this answer












                          share|improve this answer



                          share|improve this answer










                          answered Mar 25 at 2:59









                          U9-ForwardU9-Forward

                          21.7k51847




                          21.7k51847



























                              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%2f55330648%2fi-want-to-append-the-rows-of-a-dataframe-as-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







                              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