![](/img/trans.png)
[英]Aggregate Function to dataframe while retaining rows in Pandas
[英]Shuffling rows in a Pandas DataFrame while retaining the index
我目前正在嘗試找到一種方法來按行隨機化 dataframe 中的項目。 我想保留列名以及索引。 我只想更改 dataframe 中的條目順序。
目前,我正在使用
data = data.sample(frac=1).reset_index(drop=True)
但是,這會導致 output 出現一些問題。 我不認為行被正確洗牌。 還有其他方法可以實現嗎?
問題是我正在做文本分析,當我查看每個 class 最相關的一元和二元時,我得到了洗牌和原始數據的不同答案。
這是我用於字母組合和雙字母組合的代碼
tfidf = TfidfVectorizer(sublinear_tf=True,
min_df=5,
stop_words=STOPWORDS,
norm = 'l2',
encoding='latin-1',
ngram_range=(1, 2))
feat = tfidf.fit_transform(data['Combine']).toarray()
N = 5 # Number of examples to be listed
for f, i in sorted(category_labels.items()):
chi2_feat = chi2(feat, labels == i)
indices = np.argsort(chi2_feat[0])
feat_names = np.array(tfidf.get_feature_names())[indices]
unigrams = [w for w in feat_names if len(w.split(' ')) == 1]
bigrams = [w for w in feat_names if len(w.split(' ')) == 2]
print("\nFlair '{}':".format(f))
print("Most correlated unigrams:\n\t. {}".format('\n\t. '.join(unigrams[-N:])))
print("Most correlated bigrams:\n\t. {}".format('\n\t. '.join(bigrams[-N:])))
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.