Python Forum
How to move multiple columns to initial position
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
How to move multiple columns to initial position
#1
Hi,
I have below df and want to move multiple columns to initial position.

df:
wat tey step  low tool tech  category subj name
1    2     3      4    5     6      7            8    9
6    2     7      4    8     6      7            8    9

name	subj	tool	category wat	 tey	step	low  tech
9	        8	     5	     7	            1	   2	  3	  4     6
9	        8	     8	     7	            6	   2	   7	  4	 6
I use below code:

first_column = ['name',subj','tool','category ']
df_out = df[first_column].shift[-1]
But it didn't work. Can anyone please suggest how to achive that. first_column, other column order does not matter.
Reply
#2
I don't think shift does what you think it does. You are creating a dataframe that only contains 'name', subj', 'tool' and 'category and shifting the rows up 1 so the new dataframe still has 2 rows, but the bottom row is NaN.

You cancreate a new dataframe from the old with all the columns rearranged.
import pandas as pd

df = pd.DataFrame({"A":[1, 2, 3], "B":[4, 5, 6], "C":[7, 8, 9]})
print(df)

df = df[["C", "A", "B"]]
print(df)
Output:
A B C 0 1 4 7 1 2 5 8 2 3 6 9 C A B 0 7 1 4 1 8 2 5 2 9 3 6
You can use pop() and insert() to modify the existing dataframe.
import pandas as pd

df = pd.DataFrame({"A":[1, 2, 3], "B":[4, 5, 6], "C":[7, 8, 9]})
print(df)
column = df.pop("C")
df.insert(0, "C", column)
print(df)
Output:
A B C 0 1 4 7 1 2 5 8 2 3 6 9 C A B 0 7 1 4 1 8 2 5 2 9 3 6
Reply
#3
I do pop, but it only allow one column at a time. Can we do pop() for multiple columns?

(Jul-02-2022, 03:02 AM)deanhystad Wrote: I don't think shift does what you think it does. You are creating a dataframe that only contains 'name', subj', 'tool' and 'category and shifting the rows up 1 so the new dataframe still has 2 rows, but the bottom row is NaN.

You cancreate a new dataframe from the old with all the columns rearranged.
import pandas as pd

df = pd.DataFrame({"A":[1, 2, 3], "B":[4, 5, 6], "C":[7, 8, 9]})
print(df)

df = df[["C", "A", "B"]]
print(df)
Output:
A B C 0 1 4 7 1 2 5 8 2 3 6 9 C A B 0 7 1 4 1 8 2 5 2 9 3 6
You can use pop() and insert() to modify the existing dataframe.
import pandas as pd

df = pd.DataFrame({"A":[1, 2, 3], "B":[4, 5, 6], "C":[7, 8, 9]})
print(df)
column = df.pop("C")
df.insert(0, "C", column)
print(df)
Output:
A B C 0 1 4 7 1 2 5 8 2 3 6 9 C A B 0 7 1 4 1 8 2 5 2 9 3 6
Reply
#4
I also tried create new data frame, but the problem is I do not now some time coulmns are more and some time less. but the columns I want to shift lways exists. Other columns are random.

(Jul-02-2022, 03:02 AM)deanhystad Wrote: I don't think shift does what you think it does. You are creating a dataframe that only contains 'name', subj', 'tool' and 'category and shifting the rows up 1 so the new dataframe still has 2 rows, but the bottom row is NaN.

You cancreate a new dataframe from the old with all the columns rearranged.
import pandas as pd

df = pd.DataFrame({"A":[1, 2, 3], "B":[4, 5, 6], "C":[7, 8, 9]})
print(df)

df = df[["C", "A", "B"]]
print(df)
Output:
A B C 0 1 4 7 1 2 5 8 2 3 6 9 C A B 0 7 1 4 1 8 2 5 2 9 3 6
You can use pop() and insert() to modify the existing dataframe.
import pandas as pd

df = pd.DataFrame({"A":[1, 2, 3], "B":[4, 5, 6], "C":[7, 8, 9]})
print(df)
column = df.pop("C")
df.insert(0, "C", column)
print(df)
Output:
A B C 0 1 4 7 1 2 5 8 2 3 6 9 C A B 0 7 1 4 1 8 2 5 2 9 3 6
Reply
#5
The documentation for pop says the "item" is label of the column to be popped, not a list of labels. Trying to pop a list results in an index error.

https://pandas.pydata.org/pandas-docs/st...e.pop.html

But you can pop multiple items one at a time.
import pandas as pd

df = pd.DataFrame({
    "A":[1, 2, 3],
    "B":[4, 5, 6], 
    "C":[7, 8, 9],
    "D":[10, 11, 12]})

for label in ["B", "D"]:
    df.insert(0, label, df.pop(label))
print(df)
Output:
D B A C 0 10 4 1 7 1 11 5 2 8 2 12 6 3 9
DataFrame.columns() returns an iterator that you can use to get all the column labels. Here I use this to create a reorganized column list.
import pandas as pd

df = pd.DataFrame({
    "A":[1, 2, 3],
    "B":[4, 5, 6], 
    "C":[7, 8, 9],
    "D":[10, 11, 12]})

labels = ["D", "B"]
extras = [col for col in df.columns if col not in labels]
df = df[labels + extras]
print(df)
Output:
D B A C 0 10 4 1 7 1 11 5 2 8 2 12 6 3 9
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  How to check multiple columns value within range SamLiu 2 1,137 Mar-13-2023, 09:32 AM
Last Post: SamLiu
  Changing the initial worksheet name in an MS Excel file azizrasul 3 943 Oct-02-2022, 07:56 PM
Last Post: azizrasul
  df column aggregate and group by multiple columns SriRajesh 0 1,035 May-06-2022, 02:26 PM
Last Post: SriRajesh
  Split single column to multiple columns SriRajesh 1 1,319 Jan-07-2022, 06:43 PM
Last Post: jefsummers
  Apply fillna to multiple columns in dataframe rraillon 2 2,419 Aug-05-2021, 01:11 PM
Last Post: rraillon
  Pandas: how to split one row of data to multiple rows and columns in Python GerardMoussendo 4 6,797 Feb-22-2021, 06:51 PM
Last Post: eddywinch82
  How to fill parameter with data from multiple columns CSV file greenpine 1 1,653 Dec-21-2020, 06:50 PM
Last Post: Larz60+
  Variables being overridden to initial values. p2bc 6 2,617 Oct-10-2020, 09:03 PM
Last Post: p2bc
  convert string into multiple columns in python3 VAN 2 2,752 Sep-26-2020, 11:14 PM
Last Post: scidam
  How to melt dataframe multiple columns to one column Mekala 1 2,866 Sep-24-2020, 08:32 PM
Last Post: scidam

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020