# shifting specific column to before/after specific column in dataframe

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

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.

• 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! – Theinzaw Mar 18 '19 at 7:39
• I have edited the answer. – bkshi Mar 18 '19 at 11:40

getting column index , the position before that col.

x=df.columns.get_loc('col')


list of columns that are wanted to move or shift if the column are multiples and also they are in sequence index column.

var_list=new_var

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]


Because you know the names of the columns that you want, it is simple to just pull them to the front and put all other columns just as they were, after your target columns.

Get column names you care about:

desired_cols = ['medcine_preg_oth1' 'medcine_preg_oth2' 'medcine_preg_oth3']


Now get all column names and remove the ones you care about, so we only have the remainder left. There are a couple of ways to do this, but I like sets...

all_cols = set(df.columns)    # get the column names
other_cols = all_cols.difference(desired_cols)    # ones you don't care about


Now we just need to re-arrange all the columns so your desired columns are at the beginning:

tidy_cols = list(desired_cols) + list(other_cols)
tidy_df = df[tidy_cols]


I would also say this is generally not a significant action, because it only helps when printing a dataframe, for example. Otherwise all columns as still available by name as you require them.