7 examples of 'pandas sort by column name' in Python

Every line of 'pandas sort by column name' code snippets is scanned for vulnerabilities by our powerful machine learning engine that combs millions of open source libraries, ensuring your Python code is secure.

All examples are scanned by Snyk Code

By copying the Snyk Code Snippets you agree to
this disclaimer
31def column_based_sort(array, column=0):
32 """
33 >>> column_based_sort([(5, 1), (4, 2), (3, 0)], 1)
34 [(3, 0), (5, 1), (4, 2)]
35 """
36 return sorted(array, key=lambda x: x[column])
Important

Use secure code every time

Secure your code as it's written. Use Snyk Code to scan source code in minutes – no build needed – and fix issues immediately. Enable Snyk Code

162def SortColumn(self, col, reindex):
163 """
164 overridden
165 col -> sort the data based on the column indexed by col
166 """
167 self.data = self.data[self.data[:,col].argsort()]
168 if reindex: #changed so the row index is changed with sorting
169 al = len(self.data)
170 indices = np.arange(0,al)
171 self.data[:,0] = indices
84def sort_by(self, column_name, desc=False):
85 """Sort table by ``column_name``.
86
87 Sort key for each row is computed from :meth:`Row.sort_key_for_column`.
88
89 If ``desc`` is ``True``, sort order is reversed.
90
91 If present, header and footer will always be first and last, respectively.
92 """
93 if self._header is not None:
94 self._rows.pop(0)
95 if self._footer is not None:
96 self._rows.pop()
97 key = lambda row: row.sort_key_for_column(column_name)
98 self._rows.sort(key=key, reverse=desc)
99 if self._header is not None:
100 self._rows.insert(0, self._header)
101 if self._footer is not None:
102 self._rows.append(self._footer)
20def sort(iter, col, dir="ASC"):
21 if dir == "ASC":
22 reversed = False
23 elif dir == "DESC":
24 reversed = True
25
26 return sorted(iter, key=lambda r:r[col], reverse=reversed)
481def sort_key_for_column(self, column_name):
482 """(Virtual) Return the value that is to be used to sort by column ``column_name``.
483
484 By default, looks for an attribute with the same name as ``column_name``, but with an
485 underscore prefix ("unformatted value"). If there's none, tries without the underscore. If
486 there's none, raises ``AttributeError``.
487 """
488 try:
489 return getattr(self, '_' + column_name)
490 except AttributeError:
491 return getattr(self, column_name)
93def sort_by_column_id(self, col_id, method = 'asc'):
94 """Sorts the table (in-place) according to data stored in col_id
95 """
96 try:
97 col_index = self.col_names.index(col_id)
98 self.sort_by_column_index(col_index, method)
99 except ValueError:
100 raise KeyError("Col (%s) not found in table" % (col_id))
662def sort_long_df(df, ordered_genomes):
663 """sorts a long form dataframe by ordered genomes"""
664 ordered_index = dict(zip(ordered_genomes, range(len(ordered_genomes))))
665 df['order'] = df['genome'].map(ordered_index)
666 df = df.sort_values('order')
667 return df.drop('order', axis=1)

Related snippets