Sort one column of text file by keeping the other column adjust itself as per original one entity of that column.

25 views
Skip to first unread message

Savin Beniwal

unread,
Dec 18, 2019, 11:09:04 AM12/18/19
to spyder
Hi all
After spending many hours on Google, I came here to get your suggestions regarding the following query:

I have a text file of five columns. I want to sort the second column (ascending order) by keeping the other column adjust itself as per original one entity of 2nd column. I'm able to sort only one column in python but not able to keep the other column adjust itself.
I need the output as shown below:
2 0.1 0.5 6 0
1 0.2 0.5 4 5
3 0.8 0.2 5 2
5 0.8 0.2 5 2
5 2.0 0.2 3 5
8 3.0 0.9 3 6


Thank you for your time and reply (in advance).
sort_savin.txt

Brian Olsen

unread,
Dec 18, 2019, 12:36:44 PM12/18/19
to spyd...@googlegroups.com

--
You received this message because you are subscribed to the Google Groups "spyder" group.
To unsubscribe from this group and stop receiving emails from it, send an email to spyderlib+...@googlegroups.com.
To view this discussion on the web visit https://groups.google.com/d/msgid/spyderlib/f2824e35-bafe-478a-bf75-bf54bc81de75%40googlegroups.com.

Michiel Karrenbelt

unread,
Dec 18, 2019, 12:36:44 PM12/18/19
to spyder
this is not the place for this. You should checkout pandas

import pandas as pd
df = pd.read_csv('~/Downloads/sort_savin.txt', sep=' ', header=None).sort_values(by=1)

Savin Beniwal

unread,
Dec 18, 2019, 12:55:44 PM12/18/19
to spyder
Sorry Michiel Karrenbelt. I knew that this is the only place to get exact information regarding python. Anyway thanks for your time and response.
Best
------
Reply all
Reply to author
Forward
0 new messages