Follow

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use
Contact

Sorting Column in Pandas dataframe-Python

I have a dataframe that looks like this:

class_id dims
94369_GA_30122 95
27369_GA_30122 14
78369_CA_30122 27
30472_MN_55121 16

and the dataframe goes on… I want to sort my column class_id numerically ascending, that is itt must look like

class_id dims
27369_GA_30122 14
30472_MN_55121 16
78369_CA_30122 27
94369_GA_30122 95

can anyone tell me how can I achieve this?

MEDevel.com: Open-source for Healthcare and Education

Collecting and validating open-source software for healthcare, education, enterprise, development, medical imaging, medical records, and digital pathology.

Visit Medevel

>Solution :

I believe this should do the trick:

data = {"class_id": ["94369_GA_30122", "27369_GA_30122", "78369_CA_30122", "30472_MN_55121"],
        "dims": [95, 14, 27, 16]}
df = pd.DataFrame(data)

df = df.sort_values("class_id")

Out:
         class_id  dims
1  27369_GA_30122    14
3  30472_MN_55121    16
2  78369_CA_30122    27
0  94369_GA_30122    95

Edit:
You can also add these lines to only sort on the first set of numbers.

df["sorting"] = df["class_id"].str.split("_", n=1).str[0]    # Extracting only the first set of numbers
df = df.sort_values("sorting")
df = df.drop("sorting", axis=1)    # To drop the column again

https://pandas.pydata.org/docs/reference/api/pandas.Series.str.split.html

Add a comment

Leave a Reply

Keep Up to Date with the Most Important News

By pressing the Subscribe button, you confirm that you have read and are agreeing to our Privacy Policy and Terms of Use

Discover more from Dev solutions

Subscribe now to keep reading and get access to the full archive.

Continue reading