python將一個詞語list按照詞頻表DataFrame由大到小的順序排列
阿新 • • 發佈:2018-11-08
python如何將一個詞語list按照詞頻表DataFrame由大到小的順序排列?
思想就是:
首先,將按照由大到小順序排列的詞頻表DataFrame詞語抽出轉化為list
其次,將將要排列的list轉為DataFrame,並將其行名命名為list值
最後,按照將要排序list轉化後的DataFrame行名與詞頻錶轉化後的list進行匹配
# transfor message_filter_words into DataFrame
def message_filter_words_df(message_filter_words):
message_filter_wordsT = DataFrame(message_filter_words)
message_filter_wordsT.index = message_filter_wordsT[0]
return message_filter_wordsT
# transfor words_freq_table[0] into List
def words_freq_table_list(words_freq_table):
words_ls = list(words_freq_table[0])
temp_arr = np.array(words_ls)
temp_arr[temp_arr == "''"] = 'NONE'
words_freq_table_ls = list(temp_arr)
return words_freq_table_ls
# get ordered filter message
def get_template(message_filter_wordsT ,words_freq_table_ls):
message_template = list(message_filter_wordsT.reindex(words_freq_table_ls).dropna(axis=0,how='any').index)
return message_template