按特定值排序
Get sorted in specific value
我有一个包含文本及其 TFIDF 分数的列表:
[['penguji', 117 0.594552 Name: rank, dtype: float64],
['saat', 126 0.263102 Name: rank, dtype: float64],
['melakukan', 92 0.223703 Name: rank, dtype: float64],
['ini', 60 0.772189 Name: rank, dtype: float64],
['kebenaran', 73 0.318094 Name: rank, dtype: float64],
['selain', 136 0.234254 Name: rank, dtype: float64],
['khusus', 76 0.553491 Name: rank, dtype: float64],
['dan', 31 0.782068 Name: rank, dtype: float64],...]
然后我想对分数进行排序,所以我这样编码:
TFIDF_Score.sort()
print('Sorted list:', TFIDF_Score)
结果是排序的文本,而实际上,预期输出是排序的分数,如下所示:
[['dan', 31 0.782068 Name: rank, dtype: float64],
['ini', 60 0.772189 Name: rank, dtype: float64],
['penguji', 117 0.594552 Name: rank, dtype: float64],
['khusus', 76 0.553491 Name: rank, dtype: float64],
['kebenaran', 73 0.318094 Name: rank, dtype: float64],
['saat', 126 0.263102 Name: rank, dtype: float64],
['selain', 136 0.234254 Name: rank, dtype: float64],
['melakukan', 92 0.223703 Name: rank, dtype: float64],...]
如何处理循环?还是谢谢
尝试使用 sorted
:
print(sorted(TFIDF_Score, key=lambda x: -x[2]))
假设第二个元素是 pandas.Series
,使用 sorted
和 key
:
import pandas as pd
l = [['aaa', pd.Series([0.2])],
['bbb', pd.Series([0.1])]]
sorted(l, key=lambda x: x[1].values, reverse=True)
输出:
[['aaa', 0 0.2 dtype: float64],
['bbb', 0 0.1 dtype: float64]]
我有一个包含文本及其 TFIDF 分数的列表:
[['penguji', 117 0.594552 Name: rank, dtype: float64],
['saat', 126 0.263102 Name: rank, dtype: float64],
['melakukan', 92 0.223703 Name: rank, dtype: float64],
['ini', 60 0.772189 Name: rank, dtype: float64],
['kebenaran', 73 0.318094 Name: rank, dtype: float64],
['selain', 136 0.234254 Name: rank, dtype: float64],
['khusus', 76 0.553491 Name: rank, dtype: float64],
['dan', 31 0.782068 Name: rank, dtype: float64],...]
然后我想对分数进行排序,所以我这样编码:
TFIDF_Score.sort()
print('Sorted list:', TFIDF_Score)
结果是排序的文本,而实际上,预期输出是排序的分数,如下所示:
[['dan', 31 0.782068 Name: rank, dtype: float64],
['ini', 60 0.772189 Name: rank, dtype: float64],
['penguji', 117 0.594552 Name: rank, dtype: float64],
['khusus', 76 0.553491 Name: rank, dtype: float64],
['kebenaran', 73 0.318094 Name: rank, dtype: float64],
['saat', 126 0.263102 Name: rank, dtype: float64],
['selain', 136 0.234254 Name: rank, dtype: float64],
['melakukan', 92 0.223703 Name: rank, dtype: float64],...]
如何处理循环?还是谢谢
尝试使用 sorted
:
print(sorted(TFIDF_Score, key=lambda x: -x[2]))
假设第二个元素是 pandas.Series
,使用 sorted
和 key
:
import pandas as pd
l = [['aaa', pd.Series([0.2])],
['bbb', pd.Series([0.1])]]
sorted(l, key=lambda x: x[1].values, reverse=True)
输出:
[['aaa', 0 0.2 dtype: float64],
['bbb', 0 0.1 dtype: float64]]