[PYTHON] Swap columns in pandas dataframes

To replace the columns of the pandas DataFrame, use ix to specify the column names in the order after the replacement.

Example

df
'''
Suppose this is a dataframe df
    a   b   c   
A   0   1   2   
S   5   6   7   
'''

#When I run this code ...
df.ix[:,['a','c','b']]
'''
It will be as follows
    a   c   b   
A   0   2   1
S   5   7   6
'''

That's all for the points I stumbled upon as a pandas beginner.

Postscript: Of course, you can specify the column index numerically.

Recommended Posts

Swap columns in pandas dataframes
Load csv with duplicate columns in pandas
Adding Series to columns in python pandas
Add totals to rows and columns in pandas
Learn Pandas in 10 minutes
UnicodeDecodeError in pandas read_csv
Rename table columns in Django3
[Python] Plotly draws Pandas dataframes in one shot with Cufflinks
How to create dataframes and mess with elements in pandas
Create dummy variables in pandas (get_dummies)
How to write soberly in pandas
Features of pd.NA in pandas 1.0.0 (rc0)
Grammar summary often used in pandas
Dynamically create new dataframes with pandas
Extract specific multiple columns with pandas
Ignore # line and read in pandas
How to change multiple columns of csv in Pandas (Unixtime-> Japan Time)
Reading from text files and SQLite in Python (+ Pandas), R, Julia (+ DataFrames)