我有两个dataframe,我想用其中一个dataframe过滤另一个并生成一个新的dataframe。这两个数据帧有一列信息相似,但并不完全匹配。我一直在尝试使用str.contains
,但到目前为止,我一直在尝试使用TypeError: 'Series' objects are mutable, thus they cannot be hashed
。这是我的数据帧和我尝试过的代码的示例。在
promoter = pd.read_csv('promoter_coordinate.csv')
print(promoter.head())
AssociatedGeneName B C D E F
plexB_1 NC_004353.3 64381 - Drosophila melanogaster (Fruit fly) region
ci_1 NC_004353.3 76925 - Drosophila melanogaster (Fruit fly) region
RS3A_1 NC_004353.3 87829 - Drosophila melanogaster (Fruit fly) region
pan_1 NC_004353.3 89986 + Drosophila melanogaster (Fruit fly) region
pan_2 NC_004353.3 90281 + Drosophila melanogaster (Fruit fly) region
data = pd.read_csv('FBgn with gene name.csv')
print(data.head())
Gene AssociatedGeneName FBgn Number timepoint
CG10002 fkh FBgn0000659 2
CG10002 fkh FBgn0000659 2
CG10002 fkh FBgn0000659 2
CG10002 fkh FBgn0000659 2
CG10006 CG10006 FBgn0036461 2
x = promoter[promoter['AssociatedGeneName'].str.contains(data['AssociatedGeneName'])]
两个列表的标题没有匹配,但基本上理想的结果是类似于下面的内容,将比较名为“AssociatedGeneName”的两个列。在
^{pr2}$本质上,我想要一个包含promoter
中所有值与data['AssociatedGeneName']
中的值部分匹配的数据帧,如果有人能为我指出正确的方向,我将不胜感激。我对编码还比较陌生,我一直在使用python和pandas,希望继续使用python来解决这个问题。这是我一直得到的错误。在
x = promoter[promoter['AssociatedGeneName'].str.contains(data['AssociatedGeneName'])]
Traceback (most recent call last):
File "<pyshell#15>", line 1, in <module>
x = promoter[promoter['AssociatedGeneName'].str.contains(data['Associated Gene Name'])]
File "C:\Python34\lib\site-packages\pandas\core\strings.py", line 1226, in contains
na=na, regex=regex)
File "C:\Python34\lib\site-packages\pandas\core\strings.py", line 203, in str_contains
regex = re.compile(pat, flags=flags)
File "C:\Python34\lib\re.py", line 219, in compile
return _compile(pattern, flags)
File "C:\Python34\lib\re.py", line 278, in _compile
return _cache[type(pattern), pattern, flags]
File "C:\Python34\lib\site-packages\pandas\core\generic.py", line 663, in __hash__
' hashed'.format(self.__class__.__name__))
TypeError: 'Series' objects are mutable, thus they cannot be hashed
str.contains
接受一个字符串作为参数,并检查该字符串是否包含在每个promoter.AssociatedGene
项中,然后为每个索引(行)返回True
或{但是,当您将
data.AssociatedGene
传递给str.contains
函数时,您传递的是一个pandas.Series
,这就是您收到错误的原因。在如果您只需要启动程序具有部分匹配的行,则可以
现在,
^{pr2}$where_inds_par
的每个元素本身就是一个长度为>= 0
的索引数组。另外,由于您的data.AssociatedGeneName
列是冗余的,因此会有一些冗余,但是您可以使用set
和一些花哨的列表理解来过滤掉首先创建一个函数来检查来自
promoter
的值是否与data
的部分匹配,这将检查data
中的每个值这将是函数的结果
^{pr2}$然后在最后检查是否至少有一个值为true,然后返回true并过滤
promoter
相关问题 更多 >
编程相关推荐