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

                              SQL error code 1064 with creating Laravel foreign keysForeign key constraints: When to use ON UPDATE and ON DELETEDropping column with foreign key Laravel error: General error: 1025 Error on renameLaravel SQL Can't create tableLaravel Migration foreign key errorLaravel php artisan migrate:refresh giving a syntax errorSQLSTATE[42S01]: Base table or view already exists or Base table or view already exists: 1050 Tableerror in migrating laravel file to xampp serverSyntax error or access violation: 1064:syntax to use near 'unsigned not null, modelName varchar(191) not null, title varchar(191) not nLaravel cannot create new table field in mysqlLaravel 5.7:Last migration creates table but is not registered in the migration table

                              은진 송씨 목차 역사 본관 분파 인물 조선 왕실과의 인척 관계 집성촌 항렬자 인구 같이 보기 각주 둘러보기 메뉴은진 송씨세종실록 149권, 지리지 충청도 공주목 은진현