0

So the title is a bit confusing but essentially, I have a Dataframe with two columns, one for the the character ("c") and one for the character's coordinates ("loc"). I would like to sort the dataframe by the Y coordinate. So far i have managed to sort the dataframe by the X cooridate using the sort_values() function:

df = pd.DataFrame({"c":["i", "a"," d","m"], "loc":[[1, 2], [3, 3], [4, 2], [3,5]]})
df.sort_values(by=["loc"], inplace=True)

which outputs:

    c   loc
0   i  [1, 2]
1   a  [3, 3]
3   m  [3, 5]
2   d  [4, 2]

The output I am aiming for is:

    c   loc
0   i  [1, 2]
2   d  [4, 2]
1   a  [3, 3]
3   m  [3, 5]

Cycling through the dataframe and inversing the y and x values is not an option as the full dataframe will be quite large. I do think this should be possible as the new version of pd.df.sort_values() has a "key" input (link to pd.df.sort_values() documentation), but I am not sufficiently familiar with the "key" input to properly execute this.

1 Answer 1

3

Use key parameter in sort_values:

df.sort_values(by ='loc', key=lambda x: x.str[1])

Output:

    c     loc
0   i  [1, 2]
2   d  [4, 2]
1   a  [3, 3]
3   m  [3, 5]
Sign up to request clarification or add additional context in comments.

3 Comments

Quite similarly with argsort: df.iloc[df['loc'].str[1].argsort()]
How would i then add for the second order of sorting is the X coordinate? ideally with Y in the descending order and X in ascending?
@benduvi20 At this point I think you best separating X and Y in to columns in the dataframe then use sort_values with a list and a boolean list for ascending.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.