shifting specific column to before/after specific column in dataframeCreating new columns by iterating over rows in pandas dataframeReplacing column values in PandasHow can I fill NaN values in a pandas data frame?Imputation of missing values and dealing with categorical valueshow many rows have values from the same columns pandasHow to use LSTM to make prediction with both feature from the past and the current ones?Resampling pandas Dataframe keeping other columnsPandas Conditional Fill NaN Forward/BackwardHow can I merge 2+ DataFrame objects without duplicating column names?Merging dataframes in Pandas is taking a surprisingly long time

Is it ethical to recieve stipend after publishing enough papers?

Doesn't the system of the Supreme Court oppose justice?

Microchip documentation does not label CAN buss pins on micro controller pinout diagram

PTIJ: Why is Haman obsessed with Bose?

What features enable the Su-25 Frogfoot to operate with such a wide variety of fuels?

US tourist/student visa

How to make money from a browser who sees 5 seconds into the future of any web page?

Is there a RAID 0 Equivalent for RAM?

Is this part of the description of the Archfey warlock's Misty Escape feature redundant?

Does the reader need to like the PoV character?

What do you call a word that can be spelled forward or backward forming two different words

Taxes on Dividends in a Roth IRA

How much theory knowledge is actually used while playing?

How would you translate "more" for use as an interface button?

Is this toilet slogan correct usage of the English language?

Isometries between spherical space forms

I found an audio circuit and I built it just fine, but I find it a bit too quiet. How do I amplify the output so that it is a bit louder?

When were female captains banned from Starfleet?

Review your own paper in Mathematics

Are Captain Marvel's powers affected by Thanos breaking the Tesseract and claiming the stone?

Which Article Helped Get Rid of Technobabble in RPGs?

Why is so much work done on numerical verification of the Riemann Hypothesis?

How do I fix the group tension caused by my character stealing and possibly killing without provocation?

Non-trope happy ending?



shifting specific column to before/after specific column in dataframe


Creating new columns by iterating over rows in pandas dataframeReplacing column values in PandasHow can I fill NaN values in a pandas data frame?Imputation of missing values and dealing with categorical valueshow many rows have values from the same columns pandasHow to use LSTM to make prediction with both feature from the past and the current ones?Resampling pandas Dataframe keeping other columnsPandas Conditional Fill NaN Forward/BackwardHow can I merge 2+ DataFrame objects without duplicating column names?Merging dataframes in Pandas is taking a surprisingly long time













0












$begingroup$


In dataframe example :



 medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
0 Berplex Berplex None None
1 NaN NaN NaN NaN
2 NaN NaN NaN NaN
3 obmin obmin None None
4 NaN NaN NaN NaN


'medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3' ,these three columns are in somewhere of dataframe with other columns.



I want to shift these three : medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3 to the place of after 'medcine_preg_oth'.



My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose .
please suggest me!
Thanks










share|improve this question









New contributor




Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
Check out our Code of Conduct.







$endgroup$
















    0












    $begingroup$


    In dataframe example :



     medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
    0 Berplex Berplex None None
    1 NaN NaN NaN NaN
    2 NaN NaN NaN NaN
    3 obmin obmin None None
    4 NaN NaN NaN NaN


    'medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3' ,these three columns are in somewhere of dataframe with other columns.



    I want to shift these three : medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3 to the place of after 'medcine_preg_oth'.



    My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose .
    please suggest me!
    Thanks










    share|improve this question









    New contributor




    Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
    Check out our Code of Conduct.







    $endgroup$














      0












      0








      0





      $begingroup$


      In dataframe example :



       medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
      0 Berplex Berplex None None
      1 NaN NaN NaN NaN
      2 NaN NaN NaN NaN
      3 obmin obmin None None
      4 NaN NaN NaN NaN


      'medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3' ,these three columns are in somewhere of dataframe with other columns.



      I want to shift these three : medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3 to the place of after 'medcine_preg_oth'.



      My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose .
      please suggest me!
      Thanks










      share|improve this question









      New contributor




      Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.







      $endgroup$




      In dataframe example :



       medcine_preg_oth medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3
      0 Berplex Berplex None None
      1 NaN NaN NaN NaN
      2 NaN NaN NaN NaN
      3 obmin obmin None None
      4 NaN NaN NaN NaN


      'medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3' ,these three columns are in somewhere of dataframe with other columns.



      I want to shift these three : medcine_preg_oth1 medcine_preg_oth2 medcine_preg_oth3 to the place of after 'medcine_preg_oth'.



      My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose .
      please suggest me!
      Thanks







      python pandas data-cleaning






      share|improve this question









      New contributor




      Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.











      share|improve this question









      New contributor




      Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      share|improve this question




      share|improve this question








      edited Mar 18 at 7:00









      Toros91

      1,9942628




      1,9942628






      New contributor




      Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.









      asked Mar 18 at 6:33









      Theinzaw Theinzaw

      1




      1




      New contributor




      Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.





      New contributor





      Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.






      Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
      Check out our Code of Conduct.




















          2 Answers
          2






          active

          oldest

          votes


















          0












          $begingroup$

          If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.



          For example:



          #['a', 'b', 'c'] <-given columns order
          df = df[['c', 'b', 'a']]


          You can also use the built-in function reindex to accomplish you task as follow:



          cols = df.columns.tolist() #['a', 'b', 'c']
          new_cols = [['c', 'b', 'a']]
          df = df.reindex(columns=new_cols)


          EDIT:



          If you have a large number of columns, the problem will arise in how you get the new_cols list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:



          df.columns.get_loc("b") #1


          Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:



          cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]


          You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.






          share|improve this answer











          $endgroup$












          • $begingroup$
            Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
            $endgroup$
            – Theinzaw
            Mar 18 at 7:39










          • $begingroup$
            I have edited the answer.
            $endgroup$
            – bkshi
            Mar 18 at 11:40


















          0












          $begingroup$

          x=df.columns.get_loc('col') # getting column index , the position before that col.



          var_list=new_var # list of columns that are wanted to move or shift if the column are
          # multiples and also they are in sequence index column.
          new_position = x
          for var in var_list:
          cols = df.columns.tolist()
          column_to_move = var
          new_position += 1
          cols.insert(new_position, cols.pop(cols.index(column_to_move)))
          df = df[cols]






          share|improve this answer








          New contributor




          Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
          Check out our Code of Conduct.






          $endgroup$












            Your Answer





            StackExchange.ifUsing("editor", function ()
            return StackExchange.using("mathjaxEditing", function ()
            StackExchange.MarkdownEditor.creationCallbacks.add(function (editor, postfix)
            StackExchange.mathjaxEditing.prepareWmdForMathJax(editor, postfix, [["$", "$"], ["\\(","\\)"]]);
            );
            );
            , "mathjax-editing");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "557"
            ;
            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: false,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: null,
            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
            );



            );






            Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.









            draft saved

            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fdatascience.stackexchange.com%2fquestions%2f47496%2fshifting-specific-column-to-before-after-specific-column-in-dataframe%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












            $begingroup$

            If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.



            For example:



            #['a', 'b', 'c'] <-given columns order
            df = df[['c', 'b', 'a']]


            You can also use the built-in function reindex to accomplish you task as follow:



            cols = df.columns.tolist() #['a', 'b', 'c']
            new_cols = [['c', 'b', 'a']]
            df = df.reindex(columns=new_cols)


            EDIT:



            If you have a large number of columns, the problem will arise in how you get the new_cols list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:



            df.columns.get_loc("b") #1


            Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:



            cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]


            You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.






            share|improve this answer











            $endgroup$












            • $begingroup$
              Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
              $endgroup$
              – Theinzaw
              Mar 18 at 7:39










            • $begingroup$
              I have edited the answer.
              $endgroup$
              – bkshi
              Mar 18 at 11:40















            0












            $begingroup$

            If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.



            For example:



            #['a', 'b', 'c'] <-given columns order
            df = df[['c', 'b', 'a']]


            You can also use the built-in function reindex to accomplish you task as follow:



            cols = df.columns.tolist() #['a', 'b', 'c']
            new_cols = [['c', 'b', 'a']]
            df = df.reindex(columns=new_cols)


            EDIT:



            If you have a large number of columns, the problem will arise in how you get the new_cols list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:



            df.columns.get_loc("b") #1


            Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:



            cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]


            You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.






            share|improve this answer











            $endgroup$












            • $begingroup$
              Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
              $endgroup$
              – Theinzaw
              Mar 18 at 7:39










            • $begingroup$
              I have edited the answer.
              $endgroup$
              – bkshi
              Mar 18 at 11:40













            0












            0








            0





            $begingroup$

            If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.



            For example:



            #['a', 'b', 'c'] <-given columns order
            df = df[['c', 'b', 'a']]


            You can also use the built-in function reindex to accomplish you task as follow:



            cols = df.columns.tolist() #['a', 'b', 'c']
            new_cols = [['c', 'b', 'a']]
            df = df.reindex(columns=new_cols)


            EDIT:



            If you have a large number of columns, the problem will arise in how you get the new_cols list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:



            df.columns.get_loc("b") #1


            Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:



            cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]


            You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.






            share|improve this answer











            $endgroup$



            If I get the question correct, you just need to change the order of your columns. This can be simply done by reassigning the new order of your columns to the dataframe.



            For example:



            #['a', 'b', 'c'] <-given columns order
            df = df[['c', 'b', 'a']]


            You can also use the built-in function reindex to accomplish you task as follow:



            cols = df.columns.tolist() #['a', 'b', 'c']
            new_cols = [['c', 'b', 'a']]
            df = df.reindex(columns=new_cols)


            EDIT:



            If you have a large number of columns, the problem will arise in how you get the new_cols list. To do this you can use list indexing and slicing. Firstly get the index of columns you wnat to replace by using:



            df.columns.get_loc("b") #1


            Now suppose you have 699 columns and want to place the 100th and 200th column after the 7th one, you can do this:



            cols = cols[0:7] + [cols[100]] + [cols[200]] + cols[8:100] + cols[101:200] + cols[201:]


            You can now use this column to change the order of your columns in the above mentioned way. The expression will vary depending on your use case.







            share|improve this answer














            share|improve this answer



            share|improve this answer








            edited Mar 18 at 12:20

























            answered Mar 18 at 7:15









            bkshibkshi

            638111




            638111











            • $begingroup$
              Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
              $endgroup$
              – Theinzaw
              Mar 18 at 7:39










            • $begingroup$
              I have edited the answer.
              $endgroup$
              – bkshi
              Mar 18 at 11:40
















            • $begingroup$
              Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
              $endgroup$
              – Theinzaw
              Mar 18 at 7:39










            • $begingroup$
              I have edited the answer.
              $endgroup$
              – bkshi
              Mar 18 at 11:40















            $begingroup$
            Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
            $endgroup$
            – Theinzaw
            Mar 18 at 7:39




            $begingroup$
            Hi thanks for your suggestion ,but there are 699 columns in original dataframe , included of these four columns in somewhere of dataframe . So it would take time too!
            $endgroup$
            – Theinzaw
            Mar 18 at 7:39












            $begingroup$
            I have edited the answer.
            $endgroup$
            – bkshi
            Mar 18 at 11:40




            $begingroup$
            I have edited the answer.
            $endgroup$
            – bkshi
            Mar 18 at 11:40











            0












            $begingroup$

            x=df.columns.get_loc('col') # getting column index , the position before that col.



            var_list=new_var # list of columns that are wanted to move or shift if the column are
            # multiples and also they are in sequence index column.
            new_position = x
            for var in var_list:
            cols = df.columns.tolist()
            column_to_move = var
            new_position += 1
            cols.insert(new_position, cols.pop(cols.index(column_to_move)))
            df = df[cols]






            share|improve this answer








            New contributor




            Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
            Check out our Code of Conduct.






            $endgroup$

















              0












              $begingroup$

              x=df.columns.get_loc('col') # getting column index , the position before that col.



              var_list=new_var # list of columns that are wanted to move or shift if the column are
              # multiples and also they are in sequence index column.
              new_position = x
              for var in var_list:
              cols = df.columns.tolist()
              column_to_move = var
              new_position += 1
              cols.insert(new_position, cols.pop(cols.index(column_to_move)))
              df = df[cols]






              share|improve this answer








              New contributor




              Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
              Check out our Code of Conduct.






              $endgroup$















                0












                0








                0





                $begingroup$

                x=df.columns.get_loc('col') # getting column index , the position before that col.



                var_list=new_var # list of columns that are wanted to move or shift if the column are
                # multiples and also they are in sequence index column.
                new_position = x
                for var in var_list:
                cols = df.columns.tolist()
                column_to_move = var
                new_position += 1
                cols.insert(new_position, cols.pop(cols.index(column_to_move)))
                df = df[cols]






                share|improve this answer








                New contributor




                Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                $endgroup$



                x=df.columns.get_loc('col') # getting column index , the position before that col.



                var_list=new_var # list of columns that are wanted to move or shift if the column are
                # multiples and also they are in sequence index column.
                new_position = x
                for var in var_list:
                cols = df.columns.tolist()
                column_to_move = var
                new_position += 1
                cols.insert(new_position, cols.pop(cols.index(column_to_move)))
                df = df[cols]







                share|improve this answer








                New contributor




                Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                share|improve this answer



                share|improve this answer






                New contributor




                Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.









                answered 2 days ago









                Theinzaw Theinzaw

                1




                1




                New contributor




                Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.





                New contributor





                Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.






                Theinzaw is a new contributor to this site. Take care in asking for clarification, commenting, and answering.
                Check out our Code of Conduct.




















                    Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.









                    draft saved

                    draft discarded


















                    Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.












                    Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.











                    Theinzaw is a new contributor. Be nice, and check out our Code of Conduct.














                    Thanks for contributing an answer to Data Science Stack Exchange!


                    • 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.

                    Use MathJax to format equations. MathJax reference.


                    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%2fdatascience.stackexchange.com%2fquestions%2f47496%2fshifting-specific-column-to-before-after-specific-column-in-dataframe%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

                    Adding axes to figuresAdding axes labels to LaTeX figuresLaTeX equivalent of ConTeXt buffersRotate a node but not its content: the case of the ellipse decorationHow to define the default vertical distance between nodes?TikZ scaling graphic and adjust node position and keep font sizeNumerical conditional within tikz keys?adding axes to shapesAlign axes across subfiguresAdding figures with a certain orderLine up nested tikz enviroments or how to get rid of themAdding axes labels to LaTeX figures

                    Tähtien Talli Jäsenet | Lähteet | NavigointivalikkoSuomen Hippos – Tähtien Talli

                    Do these cracks on my tires look bad? The Next CEO of Stack OverflowDry rot tire should I replace?Having to replace tiresFishtailed so easily? Bad tires? ABS?Filling the tires with something other than air, to avoid puncture hassles?Used Michelin tires safe to install?Do these tyre cracks necessitate replacement?Rumbling noise: tires or mechanicalIs it possible to fix noisy feathered tires?Are bad winter tires still better than summer tires in winter?Torque converter failure - Related to replacing only 2 tires?Why use snow tires on all 4 wheels on 2-wheel-drive cars?