2017-05-23 155 views
1

我有一个DF:大熊猫多重条件,并添加多列

import pandas as pd 
df.head(20) 
          id ch  start  end strand 
0 10:100026072-100029645(+) 10 100026072 100029645  + 
1 10:110931880-110932381(+) 10 110931880 110932381  + 
2 10:110932431-110933096(+) 10 110932431 110933096  + 
3 10:111435307-111439556(-) 10 111435307 111439556  - 
4 10:115954439-115964883(-) 10 115954439 115964883  - 
5 10:115986231-116018509(-) 10 115986231 116018509  - 
6 10:116500106-116500762(-) 10 116500106 116500762  - 
7 10:116654355-116657389(-) 10 116654355 116657389  - 
8 10:117146840-117147002(-) 10 117146840 117147002  - 
9 10:126533798-126533971(-) 10 126533798 126533971  - 
10 10:127687390-127688824(+) 10 127687390 127688824  + 
11 10:19614164-19624369(-) 10 19614164 19624369  - 
12 10:42537888-42543687(+) 10 42537888 42543687  + 
13 10:61927486-61931038(-) 10 61927486 61931038  - 
14 10:70699779-70700206(-) 10 70699779 70700206  - 
15 10:76532243-76532565(-) 10 76532243 76532565  - 
16 10:79336852-79337034(-) 10 79336852 79337034  - 
17 10:79342487-79343173(+) 10 79342487 79343173  + 
18 10:79373277-79373447(-) 10 79373277 79373447  - 
19 10:82322045-82337358(+) 10 82322045 82337358  + 

df.shape 
(501, 5) 

>>>df.dtypes 
id  object 
ch  object 
start  object 
end  object 
strand object 
dtype: object 

问:

我想根据“开始”和“结束”列

率先执行多个操作创建两个附加栏名为

newstart newend 

desiredoperation: if strand == '+': 
        df['newstart'] = end - int(27) 
        df['newend'] = end + 2 
        elif: 
         strand == '-' 
         df['newstart'] = start - int(3) 
         df['newend'] = start + 26 

我该如何做到这一点使用熊猫,我发现链接belo但不知道如何执行它。如果任何人可以提供伪代码将建立在它。 adding multiple columns to pandas simultaneously

回答

1

可以使用np.where,2线,但可读

df['newstart'] = np.where(df.strand == '+', df.end-int(27), df.start-int(3)) 
df['newend'] = np.where(df.strand == '+', df.end+int(2), df.start+int(26)) 

    id       ch start  end  strand newstart newend 
0 10:100026072-100029645(+) 10 100026072 100029645 + 100029618 100029647 
1 10:110931880-110932381(+) 10 110931880 110932381 + 110932354 110932383 
2 10:110932431-110933096(+) 10 110932431 110933096 + 110933069 110933098 
3 10:111435307-111439556(-) 10 111435307 111439556 - 111435304 111435333 
4 10:115954439-115964883(-) 10 115954439 115964883 - 115954436 115954465 
5 10:115986231-116018509(-) 10 115986231 116018509 - 115986228 115986257 
6 10:116500106-116500762(-) 10 116500106 116500762 - 116500103 116500132 
7 10:116654355-116657389(-) 10 116654355 116657389 - 116654352 116654381 
8 10:117146840-117147002(-) 10 117146840 117147002 - 117146837 117146866 
9 10:126533798-126533971(-) 10 126533798 126533971 - 126533795 126533824 
+0

哦,我看到我需要调用numpy作为np。对不起,我编辑了我的代码,但我想你的答案仍然是一样的权利? – novicebioinforesearcher

+0

你在jupyter上工作我得到这个TypeError:不支持的操作数类型为 - 'str'和'int'' – novicebioinforesearcher

+0

所以如果我在jupyter笔记本上执行它它工作正常,但在终端上我得到上述错误。 – novicebioinforesearcher

1

做,如果你想这样做的熊猫,df.loc是一个很好的候选人:

df['newstart'] = df['start'] - 3 
df['newend'] = df['start'] + 26 
subset = df['strand'] == '+' 
df.loc[subset,'newstart']=df.loc[subset,'end']-27 
df.loc[subset,'newend']=df.loc[subset,'end']+2 

我认为继续使用熊猫来处理数据是一个好主意:它会保持代码的一致性,并且可能有更好,更短的方式来编写上面的代码。

df.loc是一个非常有用的函数来执行数据查找和处理,试图摆弄它,因为它是一个伟大的工具。

享受

+0

无法正常使用 – novicebioinforesearcher

+0

嗯,粘贴代码时我肯定错过了一些东西。我已经编辑了我的答案与工作代码(在Python 3.5中提到)。 –