ValueError:只能比较标记相同的Series对象 [英] ValueError: Can only compare identically-labeled Series objects

查看:1070
本文介绍了ValueError:只能比较标记相同的Series对象的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我的代码,无论我继续遇到什么错误并遵循所有与索引相关的解决方案,有人可以帮助我吗?

here is my code, no matters what I do I keep on getting the error and followed all the index related solutions, can anyone help me?

site = pd.read_csv('../data/survey_site.csv')
sampled = site.sample(n=1)

site = site.reset_index(drop=True)
sampled = sampled.reset_index(drop=True)

mask = site.mask(site['name'] == sampled['name'])

推荐答案

问题是site['name']sample['name']之间的比较是在两个pd.Series之间.您可以通过将其中一个作为标量来绕过它.但是,我注意到您使用了长度为1sample.我怀疑您认为,当您使用sample['name']时,它将是一个标量值.但是,它是一个长度为一个系列.因此,您只需要制作一个标量即可.

The issue is the comparison between site['name'] and sample['name'] is between two pd.Series. You can bypass that by making one of them a scalar. However, I noticed that you took a sample of length 1. I suspect you thought that when you took sample['name'] that it would be a scalar value. But instead it is a length one series. So you just need to make is a scalar.

选项1

mask = site.mask(site['name'] == sampled['name'].squeeze())

选项2

mask = site.mask(site['name'] == sampled.loc[0, 'name'])

这篇关于ValueError:只能比较标记相同的Series对象的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆