Python Forum
shifting specific column to before/after specific column in dataframe
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
shifting specific column to before/after specific column in dataframe
#1
In dataframe,
a b f g c d e i j h
0 1 1 1 1 1 1 1 1 1 1
1 0 0 1 0 0 1 1 1 1 0
2 1 1 1 1 1 0 0 0 0 1
3 1 0 1 0 0 1 1 1 1 0
4 1 1 1 1 1 0 1 0 0 1
5 1 0 1 0 0 1 1 1 1 0
6 1 1 1 1 1 0 1 0 1 1
7 0 0 0 0 0 0 1 0 1 0
8 1 1 1 1 1 1 1 1 1 1
9 0 0 0 0 0 0 0 0 1 0
10 1 1 1 1 1 1 1 1 1 1


I want to shift these three : 'c','d','e' to the place of after b' and column 'h' to place after 'g'.

My idea is shifting the specific columns to place after/ before specific columns in dataframe for wider purpose . please suggest me! Thanks
Reply
#2
You can specify the order of columns as you want, e.g.:

x=pd.DataFrame({'a': [1,2,3], 'b':[3,4,5], 'c':[5,6,7]})
x = x[['b','c','a']]
Output:
b c a 0 3 5 1 1 4 6 2 2 5 7 3
Reply
#3
In my data set,
there are 699 columns , I want to move column number : 694,695 to 26 and 27 respectively.
How is your suggestion!
Reply
#4
x=df.columns.get_loc('col') # getting column index , the place before that column

var_list=new_var # list of columns that are need to moved or order if multiple column and they are in
sequence position .
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]
Reply
#5
Luckily, Python is programming language
and we don't need to type all items manually.

Your code would be something like this:

columns = ['col%s' % k for k in range(700)] # df.columns.tolist()
col694 = columns.pop(694)
col695 = columns.pop(694)
columns.insert(26, col694)
columns.insert(27, col695)
# df = df[columns] # reorder columns
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Adding PD DataFrame column bsben 2 246 Mar-08-2024, 10:46 PM
Last Post: deanhystad
  Get an average of the unique values of a column with group by condition and assign it klllmmm 0 223 Feb-17-2024, 05:53 PM
Last Post: klllmmm
  delete specific row of entries jacksfrustration 3 326 Feb-13-2024, 11:13 PM
Last Post: deanhystad
  Help copying a column from a csv to another file with some extras g0nz0uk 3 406 Feb-01-2024, 03:12 PM
Last Post: DeaD_EyE
  Converting column of values into muliple columns of counts highland44 0 205 Feb-01-2024, 12:48 AM
Last Post: highland44
  Extracting specific file from an archive tester_V 4 428 Jan-29-2024, 06:41 PM
Last Post: tester_V
  Python code to set column width 1418 11 947 Jan-20-2024, 07:20 AM
Last Post: Pedroski55
  Why can't I copy and past only ONE specific tab? NewWorldRonin 8 701 Jan-12-2024, 06:31 PM
Last Post: deanhystad
  data validation with specific regular expression shaheen07 0 296 Jan-12-2024, 07:56 AM
Last Post: shaheen07
  Python Alteryx QS-Passing pandas dataframe column inside SQL query where condition sanky1990 0 690 Dec-04-2023, 09:48 PM
Last Post: sanky1990

Forum Jump:

User Panel Messages

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