如何使用Pandas(.map)标记?

bfhwhh0e  于 2021-08-20  发布在  Java
关注(0)|答案(2)|浏览(401)

下表中带有Pandas(.map)的标签

m2m_similarity.columns = ['MoviId 1','MoviId 2','similarity_score']
m2m_similarity.head(3)

我已经试着让标签稍微相似,相似,并且非常精确

m2m_similarity['analysis'] = m2m_similarity['similarity_score'].map({
    0.1: 'slightly-similar', 0.2: 'slightly-similar', 0.3: 'slightly-similar', 0.4: 'slightly-similar',
    0.5: 'similar', 0.6: 'similar', 0.7: 'similar', 0.8: 'similar',0.9: 'similar', 
    1.0: 'Exacly'
})
m2m_similarity.head(3)

结果是nan

a7qyws3x

a7qyws3x1#

更好的办法是:

m2m_similarity['analysis'] = m2m_similarity['similarity_score'].map(lambda s: 'Exacly' if round(s, 2) == 1 else ('similar' if round(s, 2) >= 0.5 else 'slightly-similar'))

因为它将涵盖两者之间的所有选项。不管怎么说,确保 similarity_score 你有数字,没有数字 strings ,如果它们实际上不是高精度浮点,则只显示第一个数字。

r1zk6ea1

r1zk6ea12#

尝试使用:

m2m_similarity['analysis'] = m2m_similarity['similarity_score'].replace({
    0.1: 'slightly-similar', 0.2: 'slightly-similar', 0.3: 'slightly-similar', 0.4: 'slightly-similar',
    0.5: 'similar', 0.6: 'similar', 0.7: 'similar', 0.8: 'similar',0.9: 'similar', 
    1.0: 'Exacly'
})

相关问题