Search
 
SCRIPT & CODE EXAMPLE
 

PYTHON

how to move a column to last in pandas

print(df)
   col1  col2  col3
0     1    11   111
1     2    22   222
2     3    33   333

s = df.pop('col1')
new_df = pd.concat([df, s], 1)
print(new_df)
Output:

   col2  col3  col1
0    11   111     1
1    22   222     2
2    33   333     3
Comment

python pandas shift last column to first place

cols = list(df.columns)
cols = [cols[-1]] + cols[:-1]
df = df[cols]
Comment

how to move the last column to the first column in pandas

cols = [df.columns[-1]] + [col for col in df if col != df.columns[-1]]
df = df[cols]
Comment

PREVIOUS NEXT
Code Example
Python :: django filter multiple conditions 
Python :: pandas value in series 
Python :: python number of lines in file 
Python :: python coding language 
Python :: Use a callable instead, e.g., use `dict` instead of `{}` 
Python :: Looping and counting in python 
Python :: dict map() 
Python :: identity matrix python 
Python :: python removing duplicate item 
Python :: matplotlib pie move percent 
Python :: python palindrome check 
Python :: seaborn 
Python :: WARNING: pip is configured with locations that require TLS/SSL, however the ssl module in Python is not available. 
Python :: access to specific column array numpy 
Python :: python keyboard 
Python :: get mode using python 
Python :: fizz buzz in python 
Python :: online python compiler 
Python :: get first letter of each word in string python 
Python :: if else pandas dataframe 
Python :: how to create a button using tkinter 
Python :: stack program in python3 
Python :: dataframe python 
Python :: python syntax errors 
Python :: python dictionary get value if key exists 
Python :: exponential python 
Python :: numpy randomly swap lines 
Python :: python dictionary key in range 
Python :: {:.1%} print one decimal pandas 
Python :: request post python 
ADD CONTENT
Topic
Content
Source link
Name
5+4 =