-
-
Notifications
You must be signed in to change notification settings - Fork 18.4k
ENH: DataFrame sort columns by rows: sort_values(axis=1) #13622
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Changes from all commits
970e25b
0f23615
2773cdf
f43ab2e
ea2d89e
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -3127,9 +3127,8 @@ def sort_values(self, by, axis=0, ascending=True, inplace=False, | |
kind='quicksort', na_position='last'): | ||
|
||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. can you update the |
||
axis = self._get_axis_number(axis) | ||
other_axis = 0 if axis == 1 else 1 | ||
|
||
if axis != 0: | ||
raise ValueError('When sorting by column, axis must be 0 (rows)') | ||
if not isinstance(by, list): | ||
by = [by] | ||
if com.is_sequence(ascending) and len(by) != len(ascending): | ||
|
@@ -3145,7 +3144,7 @@ def trans(v): | |
|
||
keys = [] | ||
for x in by: | ||
k = self[x].values | ||
k = self.xs(x, axis=other_axis).values | ||
if k.ndim == 2: | ||
raise ValueError('Cannot sort by duplicate column %s' % | ||
str(x)) | ||
|
@@ -3157,7 +3156,7 @@ def trans(v): | |
from pandas.core.groupby import _nargsort | ||
|
||
by = by[0] | ||
k = self[by].values | ||
k = self.xs(by, axis=other_axis).values | ||
if k.ndim == 2: | ||
|
||
# try to be helpful | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -84,7 +84,7 @@ def test_sort_values(self): | |
frame = DataFrame([[1, 1, 2], [3, 1, 0], [4, 5, 6]], | ||
index=[1, 2, 3], columns=list('ABC')) | ||
|
||
# by column | ||
# by column (axis=0) | ||
sorted_df = frame.sort_values(by='A') | ||
indexer = frame['A'].argsort().values | ||
expected = frame.ix[frame.index[indexer]] | ||
|
@@ -116,9 +116,26 @@ def test_sort_values(self): | |
self.assertRaises(ValueError, lambda: frame.sort_values( | ||
by=['A', 'B'], axis=2, inplace=True)) | ||
|
||
msg = 'When sorting by column, axis must be 0' | ||
with assertRaisesRegexp(ValueError, msg): | ||
frame.sort_values(by='A', axis=1) | ||
# by row (axis=1): GH 10806 | ||
sorted_df = frame.sort_values(by=3, axis=1) | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. throw in an There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Do you mean that the string There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. this is what the doc-string should be something like:
which we programatically generate (this is from |
||
expected = frame | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.sort_values(by=3, axis=1, ascending=False) | ||
expected = frame.reindex(columns=['C', 'B', 'A']) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.sort_values(by=[1, 2], axis='columns') | ||
expected = frame.reindex(columns=['B', 'A', 'C']) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.sort_values(by=[1, 3], axis=1, | ||
ascending=[True, False]) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.sort_values(by=[1, 3], axis=1, ascending=False) | ||
expected = frame.reindex(columns=['C', 'B', 'A']) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
msg = r'Length of ascending \(5\) != length of by \(2\)' | ||
with assertRaisesRegexp(ValueError, msg): | ||
|
@@ -133,6 +150,11 @@ def test_sort_values_inplace(self): | |
expected = frame.sort_values(by='A') | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.copy() | ||
sorted_df.sort_values(by=1, axis=1, inplace=True) | ||
expected = frame.sort_values(by=1, axis=1) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
sorted_df = frame.copy() | ||
sorted_df.sort_values(by='A', ascending=False, inplace=True) | ||
expected = frame.sort_values(by='A', ascending=False) | ||
|
@@ -179,6 +201,10 @@ def test_sort_nan(self): | |
sorted_df = df.sort_values(['A'], na_position='first', ascending=False) | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
expected = df.reindex(columns=['B', 'A']) | ||
sorted_df = df.sort_values(by=1, axis=1, na_position='first') | ||
assert_frame_equal(sorted_df, expected) | ||
|
||
# na_position='last', order | ||
expected = DataFrame( | ||
{'A': [1, 1, 2, 4, 6, 8, nan], | ||
|
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
just this sentence is fine, but use:
df.sort_values(by=...., axis=1)