【发布时间】:2018-09-23 16:31:41
【问题描述】:
我正在寻找一种通过数据降序对熊猫系列进行排序的平滑方法,然后是索引升序。我一直在查看文档和 Stackoverflow,但找不到直接的方法。
该系列有大约 5000 个条目,是使用 NLTK 进行 tf-idf 分析的结果。
但是,下面我提供了一个非常小的数据样本来说明问题。
import pandas as pd
index = ['146tf150p', 'anytime', '645', 'blank', 'anything']
tfidf = [1.000000, 1.000000, 1.000000, 0.932702, 1.000000]
tfidfmax = pd.Series(tfidf, index=index)
目前我只是将Series转换为DataFrame,重置索引,进行排序然后设置索引,但我觉得这是一个很大的弯路。
frame = pd.DataFrame(tfidfmax , columns=['data']).reset_index().sort_values(['data','index'], ascending=[False, True]).set_index(['index'])
3.02 ms ± 102 µs per loop (mean ± std. dev. of 7 runs, 100 loops each)
期待您的建议!
【问题讨论】:
标签: python python-3.x pandas sorting numpy