在Pandas数据框中应用IF条件时出错

时间:2019-06-27 11:46:39

标签: python-3.x

比较两个Pandas数据框的特定列值。如果条件显示错误则应用。否则它工作正常。      条件:      if(snp ['chr'] == Enhancer_marks ['chr'])#chr1 == chr1         在snp数据帧增强器中添加了#new列; snp ['enhancer']         如果为True:“是”,则snp ['enhancer'] = snp_df [position]位于Enhancer_marks起始列和结束列之间。和False:“否”。

 ==> dataframe1: snp <==
 snp chr position
 rs6604985   chr1   1552755
 rs75631842  chr1   100028246
 rs75631842  chr1   100028249

 ==> dataframe2: enhancer_marks <==
 chr10   100027038   100027943
 chr1    100028246   100029244
 chr1    100028246   100029244


 ==> OUTPUT <==
 snp           chr   position    enhancer
 rs6604985    chr1    1552755       False
 rs75631842   chr1  100028246        True
 rs75631842   chr1  100028249        True

```
import pandas as pd
import numpy as np

def snp_enhancer_localization(snp,enhancer_marks):
    snp = pd.read_csv(snp,delimiter='\s+',header=0)
    enhancer_marks = pd.read_csv(enhancer_marks,delimiter='\s+',names=['chr','start','end'])
    enhancer_marks['chr'] = enhancer_marks.chr.str.replace('chr' , '')

    if(snp['chr'] == enhancer_marks['chr']):
        snp['enhancer'] = snp['position'].between(enhancer_marks['start'],enhancer_marks['end'], inclusive=True)
    return snp
x=snp_enhancer_localization('snp','enhancer_marks')
print(x)
```

0 个答案:

没有答案