简体   繁体   English

来自当前df子集的新Pandas数据框

[英]New Pandas dataframe from subset of current df

I have a Pandas dataframe with town names in one column. 我有一列具有城镇名称的熊猫数据框。 I want to create a new dataframe containing a subset of those towns. 我想创建一个包含这些城镇子集的新数据框。 I have tried the following code but it errors: 我尝试了以下代码,但出现错误:

crime[(crime.Region == 'Stroud') & (crime.Region == 'Gloucester')]

The error I get is: 我得到的错误是:

'Series' objects are mutable, thus they cannot be hashed. “系列”对象是可变的,因此不能被散列。

Interestingly, this code works if I use '!=' and create a new dataframe based on stating the towns I do not want in it, however there are considerably more of these and it seems unintuitive. 有趣的是,如果我使用'!='并基于声明我不想要的城镇创建一个新的数据框,那么此代码将起作用,但是其中有很多城镇,这似乎并不直观。

我认为您正在寻找执行OR而非AND,因此您可能希望拥有类似

crime[(crime.Region == 'Stroud') | (crime.Region == 'Gloucester')]

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM