<jupyter_start><jupyter_text>Indirect Sorting with argsort<jupyter_code>import numpy as np<jupyter_output><empty_output><jupyter_text>Argsort --> returns indices that would sort an array, leaves array unchanged<jupyter_code>x = np.array([3,5,1,6,2,7])
print(np.argsort(x))
print(x[x.argsort()])
print(x)<jupyter_output>[2 4 0 1 3 5]
[1 2 3 5 6 7]
[3 5 1 6 2 7]<jupyter_text>You can use it to match up values from separate arrays<jupyter_code>grade = np.array([3, 5, 1, 6, 2, 7])
ages = np.array([8, 10, 6, 11, 7, 12])
sort_index = np.argsort(ages)
print(grade[sort_index])
print(ages[sort_index])<jupyter_output>[1 2 3 5 6 7]
[ 6  7  8 10 11 12]<jupyter_text>What if you want the comma seperators between values?<jupyter_code>print(list(grade[sort_index]))<jupyter_output>[1, 2, 3, 5, 6, 7]<jupyter_text>Can also indirectly sort in descending order<jupyter_code>print(list(ages[sort_index])[::-1])<jupyter_output>[12, 11, 10, 8, 7, 6]
{'fraction_non_alphanumeric': 0.14882226980728053, 'fraction_numerical': 0.07494646680942184, 'mean_word_length': 6.9914529914529915, 'pattern_counts': {'":': 0, '<': 17, '<?xml version=': 0, '>': 18, 'https://': 0, 'lorem ipsum': 0, 'www.': 0, 'xml': 0}, 'pii_count': 0, 'substrings_counts': 0, 'word_list_counts': {'cursed_substrings.json': 0, 'profanity_word_list.json': 0, 'sexual_word_list.json': 0, 'zh_pornsignals.json': 0}}
{'chain_length': '5', 'dir': 'jupyter-structured-clean-dedup', 'id': '221265', 'license': 'MIT', 'n_tokens_mistral': 447, 'n_tokens_neox': 384, 'n_words': 100, 'path': 'Section 1/1.3_Indirect_Sorting.ipynb', 'repo_name': 'PacktPublishing/Pandas-and-NumPy-Tips-Trick-and-Techniques', 'size': '934'}