我在这里遇到了一些问题,在我的python包中我已经安装了numpy,但是我仍然有这个错误:
排序‘DataFrame’对象没有‘
’属性‘’
任何人都能给我一些建议..
这是我的代码:
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)发布于 2017-05-23 08:17:35
对于DataFrames,sort()已弃用,取而代之的是:
sort_values() to sort by column(s)sort_index() to sort by 随着sort_values()和sort_index()的引入,sort()在Pandas 0.17 (2015-10-09)中被弃用(但仍然可用)。它从Pandas中删除,版本为0.20 (2017-05-05)。
发布于 2019-01-28 17:46:28
熊猫排序101
在v0.20中,和已经取代了sort。除此之外,我们还有argsort。
以下是一些常见的排序用例,以及如何使用当前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按单列排序
例如,要按列"A“对df进行排序,请使用带有单个列名的sort_values:
df.sort_values(by='A')
A B
0 a 7
3 a 5
4 b 2
1 c 9
2 c 3如果您需要新的RangeIndex,请使用DataFrame.reset_index。
按多列排序
例如,要在df中同时按列"A“和"B”排序,可以向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按DataFrame索引进行排序
df2 = df.sample(frac=1)
df2
A B
1 c 9
0 a 7
2 c 3
3 a 5
4 b 2您可以使用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以下是一些具有可比性的方法及其性能:
%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)按索引列表排序
例如,
idx = df2.index.argsort()
idx
# array([0, 7, 2, 3, 9, 4, 5, 6, 8, 1])这个“排序”问题实际上是一个简单的索引问题。只需将整数标签传递给iloc即可。
df.iloc[idx]
A B
1 c 9
0 a 7
2 c 3
3 a 5
4 b 2https://stackoverflow.com/questions/44123874
复制相似问题