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

how do I convert multiple columns into a list in ascending order?

I have a dataset of this type:

0 1 2
0:0 57:0 166:0
0:5 57:20 166:27
0:10 57:8: 166:36
0:27 57:4 166:45

i want to convert this dataframe into an ascending list. I want to check the whole data into ascending order and then create a list using the ascending order numbers in the dataframe. and ascending order should be of numbers before ‘:’

desired output:

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

list
0
57
166

>Solution :

You can unstack (or stack) to flatten to Series, then extract the number, convert to integer and keep the unique values in order:

For a python list you can try:

sorted(df.unstack()
         .str.extract('(\d+):', expand=False)
         .astype(int)
         .unique().tolist())

output: [0, 57, 166]

As Series:

out = (df.unstack()
         .str.extract('(\d+):', expand=False)
         .astype(int)
         .drop_duplicates()
         .sort_values().reset_index(drop=True)
      )

output:

0      0
1     57
2    166
dtype: int64
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