Python Forum
How to move multiple columns to initial position - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: General Coding Help (https://python-forum.io/forum-8.html)
+--- Thread: How to move multiple columns to initial position (/thread-37623.html)



How to move multiple columns to initial position - SriRajesh - Jul-01-2022

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.


RE: How to move multiple columns to initial position - deanhystad - Jul-02-2022

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



RE: How to move multiple columns to initial position - SriRajesh - Jul-02-2022

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



RE: How to move multiple columns to initial position - SriRajesh - Jul-02-2022

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



RE: How to move multiple columns to initial position - deanhystad - Jul-02-2022

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/stable/reference/api/pandas.DataFrame.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