Pandas sort columns by name

2024/7/27 16:11:54

I have the following dataframe, where I would like to sort the columns according to the name.

1 | 13_1 | 13_10| 13_2  | 2   | 3
9 |  31  | 2    |  1    | 3   | 4

I am trying to sort the columns in the following way:

1 |  2  | 3    | 13_1  | 13_2  | 13_10
9 |  3  | 4    |  31   |  1    | 2

I've been trying to solve this using df.sort_index(axis=1, inplace=True), however the result turns out to be the same as my initial dataframe. I.e:

1 | 13_1 | 13_10| 13_2  | 2   | 3
9 |  31  | 2    |  1    | 3   | 4

It seems it recognizes 13_1 as 1.31 and not as 13.1. Furthermore, I tried a conversion of the column names from string to float. However, this turns out to treat 13_1 and 13_10 both as 13.1 giving me duplicate column names.

Answer

natsort

from natsort import natsorteddf = df.reindex(natsorted(df.columns), axis=1)#   1  2  3  13_1  13_2  13_10
#0  9  3  4    31     1      2
https://en.xdnf.cn/q/72631.html

Related Q&A

Series objects are mutable, thus they cannot be hashed error calling to_csv

I have a large Dataframe (5 days with one value per second, several columns) of which Id like to save 2 columns in a csv file with python pandas df.to_csv module.I tried different ways but always get t…

Python client / server question

Im working on a bit of a project in python. I have a client and a server. The server listens for connections and once a connection is received it waits for input from the client. The idea is that the c…

Segmentation fault during import cv on Mac OS

Trying to compile opencv on my Mac from source. I have following CMakeCache.txt: http://pastebin.com/KqPHjBx0I make ccmake .., press c, then g. Than I make sudo make -j8: http://pastebin.com/cJyr1cEdTh…

Python bug - or my stupidity - EOL while scanning string literal

I cannot see a significant difference between the two following lines. Yet the first parses, and the latter, does not.In [5]: n=""" \\"Axis of Awesome\\" """In […

IOPub Error on Google Colaboratory in Jupyter Notebook

I understand that the below command jupyter notebook --NotebookApp.iopub_data_rate_limit=1.0e10 would let me set the data rate. But on Colab, I cannot run this command since the notebook is already ope…

Python code calls C library that create OS threads, which eventually call Python callbacks

If the one and only Python interpreter is in the middle of executing a bytecode when the OS dispatches another thread, which calls a Python callback - what happens? Am I right to be concerned about th…

Django MTMField: limit_choices_to = other_ForeignKeyField_on_same_model?

Ive got a couple django models that look like this:from django.contrib.sites.models import Siteclass Photo(models.Model):title = models.CharField(max_length=100)site = models.ForeignKey(Site)file = mod…

Logging django.request to file instead of console

I am trying to configure my django settings.py to properly use the python logging facility but Ive stumbled upon a rather strange problem:Even after reading the docs, I simply cant find out how to redi…

Coinbase APIerror(id = ) in python

I want to transfer money between my coinbase accounts. Im storing all of my accounts IDs from client.get_accounts()[data][id] and transferring with the code, tx = client.transfer_money(2bbf394c-193b-5b…

cxfreeze missing distutils module inside virtualenv

When running a cxfreeze binary from a python3.2 project I am getting the following runtime error:/project/dist/project/distutils/__init__.py:13: UserWarning: The virtualenv distutils package at %s appe…