ValueError: Can only compare identically-labeled Series objects

旧城冷巷雨未停 提交于 2019-12-24 01:23:56

问题


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'])

回答1:


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.

Option 1

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

Option 2

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


来源:https://stackoverflow.com/questions/45724817/valueerror-can-only-compare-identically-labeled-series-objects

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!