python - 在保留索引的同时对 Pandas DataFrame 中的行进行改组
问题描述
我目前正在尝试找到一种方法来按行随机化数据框中的项目。我想保留列名以及索引。我只想更改数据框中条目的顺序。
目前,我正在使用
data = data.sample(frac=1).reset_index(drop=True)
但是,这会导致输出方面的一些问题。我不认为行被正确洗牌。还有其他方法可以实现吗?
问题是我正在做文本分析,当我查看每个类中最相关的一元和二元时,我得到了对打乱数据和原始数据的不同答案。
这是我用于字母组合和双字母组合的代码
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:])))
解决方案
推荐阅读
- jupyter-notebook - 保存笔记本小部件状态并在重新加载笔记本后允许小部件交互
- r - Dplyr 警告:`...` 不为空
- r - R逐行比较列值
- vue.js - Vuetify 2.3.4 日期选择器关闭内容点击似乎是“故障”
- python - Python - 终端没有布尔显示
- sql - 试图声明一个游标,但我被文件结尾击中了!(甲骨文 SQL)
- python - python从具有重复名称的json数组中解析(mitre att&ck)
- sql - Rails 查询 has_and_belongs_to_many 精确计数
- python - 无法将输入变量与 .txt 中的数据进行比较/无法读取文件以比较数据
- python - 如何在数据框中分组并获取平均值?