python - Pandas dataframe - 识别任何列中值超过阈值的行

标签 python pandas dataframe

我有一个单词相似度矩阵,存储为 pandas 数据框,其中列是约 400 个单词的“种子集”,行索引是约 50,000 个单词的大词典。任何行/列的值是两个词之间从 0 到 1 的相似度。

>>> df_sim_mf.info()
<class 'pandas.core.frame.DataFrame'>
Index: 46265 entries, #angry to wonga
Columns: 451 entries, abandon to wrongs
dtypes: float64(451)
memory usage: 159.5+ MB
>>> df_sim_mf.sample(10).sample(5, axis = 1)
              nationality    purest     unite   lawless      riot
assaulted        0.114270 -0.140504  0.182024  0.434651  0.510618
peekaboo        -0.008734 -0.027742  0.051084  0.260245  0.201117
antibiotic       0.145310  0.270748 -0.126459 -0.083965  0.043086
killin          -0.102474  0.123550  0.055935 -0.115381  0.285997
warrior          0.005229  0.281967  0.261230  0.344130  0.359228
actionscript    -0.029405  0.077793  0.114047 -0.052599 -0.123401
controversy      0.336688  0.271007  0.373474  0.362565  0.305548
nic              0.164550 -0.159097  0.080056  0.271184  0.231357
healy            0.072831  0.102996  0.286538  0.335697  0.183730
uncovered        0.061310  0.274003  0.328383  0.300315  0.277491

我试图从我的大词典中找到所有与我的任何“种子集”在一定相似度范围内的单词。也就是说,我想选择至少包含一个大于 0.75 的值的每一行。

我可以使用一些简单的 pandas 命令来做到这一点吗?

最佳答案

你可以这样做:

df.loc[(df > 0.75).sum(axis=1) > 0, :]

如果您只需要单词,则获取 index 属性。

关于python - Pandas dataframe - 识别任何列中值超过阈值的行,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36229605/

相关文章:

python - 使用 mdates 和 matplotlib 将 x 轴设置为日期

python - Pandas df.itertuples 在打印时重命名数据框列

python-3.x - Python Pandas : Best way to normalize data?

python - 寻找一种更快的方法在包含另一列行中的字典值的数据框中创建新列

python - 如何将字符串从 df.to_string() 转换回 DataFrame

python - 使用python访问函数内部的字典

Python:删除列表列表中的重复项,忽略列表顺序

python - Amazon Lambda 是否适合每天并行计算数百份报告?

python - 获取 ord 值的总和

python - 有条件 reshape pandas