Python 'DataFrame' 对象没有属性 'sort'
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/44123874/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
'DataFrame' object has no attribute 'sort'
提问by Shi Jie Tio
I face some problem here, in my python package I have install numpy, but I still have this error 'DataFrame' object has no attribute 'sort'
我在这里遇到了一些问题,在我的 python 包中我安装了 numpy,但我仍然有这个错误“DataFrame”对象没有属性“排序”
Anyone can give me some idea..
任何人都可以给我一些想法..
This is my code :
这是我的代码:
final.loc[-1] =['', 'P','Actual']
final.index = final.index + 1 # shifting index
final = final.sort()
final.columns=[final.columns,final.iloc[0]]
final = final.iloc[1:].reset_index(drop=True)
final.columns.names = (None, None)
回答by Brad Solomon
sort()
was deprecated for DataFrames in favor of either:
sort()
已弃用 DataFrames 以支持以下任一方式:
sort_values()
to sort by column(s)sort_index()
to sort by the index
sort_values()
到由列排序(S)sort_index()
要通过索引排序
sort()
was deprecated (but still available) in Pandas with release 0.17 (2015-10-09) with the introduction of sort_values()
and sort_index()
. It was removed from Pandas with release 0.20 (2017-05-05).
sort()
在 0.17 (2015-10-09) 版本的 Pandas 中被弃用(但仍然可用)sort_values()
和sort_index()
。它在 0.20 (2017-05-05) 版本中从 Pandas 中删除。
回答by cs95
Pandas Sorting 101
熊猫排序 101
sort
has been replaced in v0.20 by DataFrame.sort_values
and DataFrame.sort_index
. Aside from this, we also have argsort
.
sort
已在 v0.20 中由DataFrame.sort_values
和替换DataFrame.sort_index
。除此之外,我们还有argsort
.
Here are some common use cases in sorting, and how to solve them using the sorting functions in the current API. First, the setup.
以下是排序中的一些常见用例,以及如何使用当前 API 中的排序函数来解决它们。首先,设置。
# Setup
np.random.seed(0)
df = pd.DataFrame({'A': list('accab'), 'B': np.random.choice(10, 5)})
df
A B
0 a 7
1 c 9
2 c 3
3 a 5
4 b 2
Sort by Single Column
按单列排序
For example, to sort df
by column "A", use sort_values
with a single column name:
例如,要按df
列“A”排序,请使用sort_values
单个列名:
df.sort_values(by='A')
A B
0 a 7
3 a 5
4 b 2
1 c 9
2 c 3
If you need a fresh RangeIndex, use DataFrame.reset_index
.
如果您需要新的 RangeIndex,请使用DataFrame.reset_index
.
Sort by Multiple Columns
按多列排序
For example, to sort by bothcol "A" and "B" in df
, you can pass a list to sort_values
:
例如,要同时按列“A”和“B”排序df
,您可以将列表传递给sort_values
:
df.sort_values(by=['A', 'B'])
A B
3 a 5
0 a 7
4 b 2
2 c 3
1 c 9
Sort By DataFrame Index
按数据帧索引排序
df2 = df.sample(frac=1)
df2
A B
1 c 9
0 a 7
2 c 3
3 a 5
4 b 2
You can do this using sort_index
:
您可以使用sort_index
以下方法执行此操作:
df2.sort_index()
A B
0 a 7
1 c 9
2 c 3
3 a 5
4 b 2
df.equals(df2)
# False
df.equals(df2.sort_index())
# True
Here are some comparable methods with their performance:
以下是一些具有性能可比性的方法:
%timeit df2.sort_index()
%timeit df2.iloc[df2.index.argsort()]
%timeit df2.reindex(np.sort(df2.index))
605 μs ± 13.6 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
610 μs ± 24.2 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
581 μs ± 7.63 μs per loop (mean ± std. dev. of 7 runs, 1000 loops each)
Sort by List of Indices
按指数列表排序
For example,
例如,
idx = df2.index.argsort()
idx
# array([0, 7, 2, 3, 9, 4, 5, 6, 8, 1])
This "sorting" problem is actually a simple indexing problem. Just passing integer labels to iloc
will do.
这个“排序”问题实际上是一个简单的索引问题。只需传递整数标签即可iloc
。
df.iloc[idx]
A B
1 c 9
0 a 7
2 c 3
3 a 5
4 b 2