Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/317.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Python 对列进行条件解析_Python_Pandas_Parsing_Lambda - Fatal编程技术网

Python 对列进行条件解析

Python 对列进行条件解析,python,pandas,parsing,lambda,Python,Pandas,Parsing,Lambda,我有一个如下所示的数据帧: CCDS Size gene exonCount 10804 NM_001320648_exon_4_0_chr21_44515804_r 50 U2AF1L5 8 9385 NM_018964_intron_9_0_chr21_43967251_f 6921 SLC37A1 21 1253 NM_004540_intron_4_0_chr21_22664562_f 32141 NCAM2 18 52

我有一个如下所示的数据帧:

    CCDS    Size    gene    exonCount
10804   NM_001320648_exon_4_0_chr21_44515804_r  50  U2AF1L5 8
9385    NM_018964_intron_9_0_chr21_43967251_f   6921    SLC37A1 21
1253    NM_004540_intron_4_0_chr21_22664562_f   32141   NCAM2   18
5233    NM_203417_intron_1_0_chr21_35893957_r   1878    RCAN1   4
3242    NM_001353691_intron_0_0_chr21_32493156_r    3684    TIAM1   30
我想解析CCDS列并引入一个名为
farme
的新列,其方式如下:

1) 如果CCDS列的最后一个字段(在基于“_”)是
f
,则
frame
的值是
df['CCDS'].str.split(''u').str[3]+1
&

2) 如果CCDS列的最后一个字段是
r
,则
值将等于
|exonCount df['CCDS'].str.split('uz').str[3]|

我曾尝试设计lambda函数,但我不确定如何详细说明
else
条件:

df['frame'] = df.Set.map( lambda x: df['CCDS'].str.split('_').str[3]+1
                         if intronExon_LDU['CCDS'].str.split('_').str[7] == 'f' 
                         else ????)
使用
np.where
(可以很容易地推广到多个/嵌套条件)

或者,
loc
带有布尔掩码

df['frame'] = v
df.loc[df['CCDS'].str.endswith('f'), 'frame'] += 1
使用
str.endswith

使用
np.where
(可以轻松推广到多个/嵌套条件)干净地生成掩码

或者,
loc
带有布尔掩码

df['frame'] = v
df.loc[df['CCDS'].str.endswith('f'), 'frame'] += 1

掩码是用
str.endswith

干净地生成的。我如何使用np.where的多个条件:
test['frame']=np.where(test['category']=='intron',test['CCDS'].str.endswith('r'),abs(v-((test['exonCount'].values)-1)))
@RezaJabal看看numpy.select。它的用法非常相似。谢谢你的帮助!我怎样才能在np.where中使用多个条件:
test['frame']=np.where(test['category']='intron'&test['CCDS'].str.endswith('r')、abs(v-((test['exincount'].values)-1))
@RezaJabal看看numpy.select。它的用法非常相似。谢谢你的帮助!