Python 如何合并给定代码中的条件以生成用于马尔可夫建模的新代码?
在以下数据(d1_组)中,我尝试运行马尔可夫模型:Python 如何合并给定代码中的条件以生成用于马尔可夫建模的新代码?,python,pandas,if-statement,lambda,markov-chains,Python,Pandas,If Statement,Lambda,Markov Chains,在以下数据(d1_组)中,我尝试运行马尔可夫模型: contig pos M1 M2 Mk Mg1 F1 F1_PG F1_block S1 Sk1 S2 Sj 2 16230484 G/G G/G G/G G/G G|T 0|1 255 T/T T/T T/T T/T 2 16230484 G/G G/G G/G G/G G|T 0|1 255 T/T T/T T/T T/T 2 16230491 C/C C/C C/C C/C C|T 0
contig pos M1 M2 Mk Mg1 F1 F1_PG F1_block S1 Sk1 S2 Sj
2 16230484 G/G G/G G/G G/G G|T 0|1 255 T/T T/T T/T T/T
2 16230484 G/G G/G G/G G/G G|T 0|1 255 T/T T/T T/T T/T
2 16230491 C/C C/C C/C C/C C|T 0|1 255 T/T T/T . T/T
2 16230503 T/T T/T T/T T/T T|T 0|1 255 T/T T/T . T/T
2 16230524 T/T T/T T/T T/T T|A 0|1 255 A/A A/A A/A A/A
2 16230535 . . . . T|C 0|1 255 . . . .
2 16232072 A/A A/A A/A A/A A|G 0|1 255 G/G G/G G/G G/G
2 16232072 A/A A/A A/A A/A A|G 0|1 255 G/G G/G G/G G/G
2 16229767 T/T T/T T/T G/T C|T 1|0 726 . T/C T/C T/C
2 16229767 T/T T/T T/T G/T C|T 1|0 726 . T/C T/C T/C
2 16229783 C/C C/C C/C A/C G|C 0|1 726 G/C G/C G/C C|G
2 16229992 A/A A/A A/A G/A G|A 1|0 726 A/A A/A A/A A|G
2 16230007 T/T T/T T/T A/T A|T 1|0 726 A|T A|T A|T A|T
2 16230011 G/G G/G G/G G/G C|G 1|0 726 G/C C|G C|G G/C
2 16230049 A/A A/A A/A A/A T|A 1|0 726 A|T . A/T A/T
2 16230174 . . . C/C T|C 1|0 726 C|T T|C T|C C|T
2 16230190 A/A A/A A/A A/A T|A 1|0 726 T|G G|T T|G T|G
2 16230260 A/A A/A A/A A/A G|A 1|0 726 G/G G/G G/G G/G
2 16230260 A/A A/A A/A A/A G|A 1|0 726 G/G G/G G/G G/G
2 16232769 A/A A/A A/A A/A T|A 1|0 1335 T/T T/T T/T T/T
2 16232769 A/A A/A A/A A/A T|A 1|0 1335 T/T T/T T/T T/T
2 16232772 A/A A/A A/A A/A C|A 1|0 1335 C/C C/C C/C C/C
2 16232793 C/C C/C C/C C/C T|C 1|0 1335 T/T T/T T/T T/T
2 16232793 C/C C/C C/C C/C T|C 1|0 1335 T/T T/T T/T T/T
2 16232282 T/T T/T T/T T/T T|A 0|1 3353 A/A A/A A/A A/A
2 16232282 T/T T/T T/T T/T T|A 0|1 3353 A/A A/A A/A A/A
2 16232444 T/T T/T T/T T/T T|C 0|1 3353 C/C C/C C/C C/C
2 16232444 T/T T/T T/T T/T T|C 0|1 3353 C/C C/C C/C C/C
我运行以下代码:
import pandas as pd
do something to import the data in pd dataframe
d1_group.set_index('pos', inplace=True)
d2_list = d1_group.applymap(lambda c:[list(c)]) # this splits the values in each column
然后根据条件进行数据分析,
我正在根据数据的条件(即列值中有斜线与管道)获取itertools产品
vs.zip
所以总的来说有三个条件:
d_mm = (d2_list+d2_list.shift(1)).dropna(how='all').\
applymap((lambda c: ','.join('g'.join(t) for t in it.product(*c) if '/' not in t if '/' in ''.join(map(str, c)) elif '|' in ''.join(map(str, c)) 'g'.join(t) for t in zip(*c) if '|' not in t if '|')))
and tried to put `elif` and `if ... not` in several different ways, but couldn't solve this nested loop problem.
S1 Sk1 S2 Sj
pos
2 16230174 .... C|T T|C T|C C|T
2 16230190 .... T|G G|T T|G T|G
16230174 .... CgA,TgT . TgA,CgT CgA,TgT
16230190 .... TgC,GgT GgT,TgC TgT,GgC TgC,GgT
条件1:
d_mm = (d2_list+d2_list.shift(1)).dropna(how='all').\
applymap((lambda c: ','.join('g'.join(t) for t in it.product(*c) if '/' not in t if '/' in ''.join(map(str, c)) elif '|' in ''.join(map(str, c)) 'g'.join(t) for t in zip(*c) if '|' not in t if '|')))
and tried to put `elif` and `if ... not` in several different ways, but couldn't solve this nested loop problem.
S1 Sk1 S2 Sj
pos
2 16230174 .... C|T T|C T|C C|T
2 16230190 .... T|G G|T T|G T|G
16230174 .... CgA,TgT . TgA,CgT CgA,TgT
16230190 .... TgC,GgT GgT,TgC TgT,GgC TgC,GgT
如果c和shift(c)都包含管道(|),即d3_mm的代码,我正在尝试获取c和shift(c)
的邮政编码
条件2:
d_mm = (d2_list+d2_list.shift(1)).dropna(how='all').\
applymap((lambda c: ','.join('g'.join(t) for t in it.product(*c) if '/' not in t if '/' in ''.join(map(str, c)) elif '|' in ''.join(map(str, c)) 'g'.join(t) for t in zip(*c) if '|' not in t if '|')))
and tried to put `elif` and `if ... not` in several different ways, but couldn't solve this nested loop problem.
S1 Sk1 S2 Sj
pos
2 16230174 .... C|T T|C T|C C|T
2 16230190 .... T|G G|T T|G T|G
16230174 .... CgA,TgT . TgA,CgT CgA,TgT
16230190 .... TgC,GgT GgT,TgC TgT,GgC TgC,GgT
但是,如果c和/或shift(c)包含(/)我想要itertools.product
即d2\u mm的代码
我需要合并d2_mm和d3_mm中的条件,从开始,对于t in…
,还需要添加第三个条件
条件3:
d_mm = (d2_list+d2_list.shift(1)).dropna(how='all').\
applymap((lambda c: ','.join('g'.join(t) for t in it.product(*c) if '/' not in t if '/' in ''.join(map(str, c)) elif '|' in ''.join(map(str, c)) 'g'.join(t) for t in zip(*c) if '|' not in t if '|')))
and tried to put `elif` and `if ... not` in several different ways, but couldn't solve this nested loop problem.
S1 Sk1 S2 Sj
pos
2 16230174 .... C|T T|C T|C C|T
2 16230190 .... T|G G|T T|G T|G
16230174 .... CgA,TgT . TgA,CgT CgA,TgT
16230190 .... TgC,GgT GgT,TgC TgT,GgC TgC,GgT
如果c和/或shift(c)包含句点(.)则该联接的输出只是一个句点,并不复杂
那么,如何在lambda函数中实现这一点呢?如果不是,没有lambda的解决方案也可以。
我尝试了类似这样的方法:
d_mm = (d2_list+d2_list.shift(1)).dropna(how='all').\
applymap((lambda c: ','.join('g'.join(t) for t in it.product(*c) if '/' not in t if '/' in ''.join(map(str, c)) elif '|' in ''.join(map(str, c)) 'g'.join(t) for t in zip(*c) if '|' not in t if '|')))
and tried to put `elif` and `if ... not` in several different ways, but couldn't solve this nested loop problem.
S1 Sk1 S2 Sj
pos
2 16230174 .... C|T T|C T|C C|T
2 16230190 .... T|G G|T T|G T|G
16230174 .... CgA,TgT . TgA,CgT CgA,TgT
16230190 .... TgC,GgT GgT,TgC TgT,GgC TgC,GgT
我还尝试使用另一个def函数
,但使用c和shift(c)
上的条件以及中的t
上的条件时,我遇到了一些复杂问题,然后如果的话,会出现过多的
预期最终输出:
d_mm = (d2_list+d2_list.shift(1)).dropna(how='all').\
applymap((lambda c: ','.join('g'.join(t) for t in it.product(*c) if '/' not in t if '/' in ''.join(map(str, c)) elif '|' in ''.join(map(str, c)) 'g'.join(t) for t in zip(*c) if '|' not in t if '|')))
and tried to put `elif` and `if ... not` in several different ways, but couldn't solve this nested loop problem.
S1 Sk1 S2 Sj
pos
2 16230174 .... C|T T|C T|C C|T
2 16230190 .... T|G G|T T|G T|G
16230174 .... CgA,TgT . TgA,CgT CgA,TgT
16230190 .... TgC,GgT GgT,TgC TgT,GgC TgC,GgT
拥有所有3-条件后我的所有列的预期输出将与此类似(如下),但每列将有4个产品或2个zip或句点,这取决于列值(用c表示)是否有斜线(/)或管道(|)或句点(.):
对于上述输出中未显示的列(给定输入位置的S1 Sj)
输出为:
输入:
d_mm = (d2_list+d2_list.shift(1)).dropna(how='all').\
applymap((lambda c: ','.join('g'.join(t) for t in it.product(*c) if '/' not in t if '/' in ''.join(map(str, c)) elif '|' in ''.join(map(str, c)) 'g'.join(t) for t in zip(*c) if '|' not in t if '|')))
and tried to put `elif` and `if ... not` in several different ways, but couldn't solve this nested loop problem.
S1 Sk1 S2 Sj
pos
2 16230174 .... C|T T|C T|C C|T
2 16230190 .... T|G G|T T|G T|G
16230174 .... CgA,TgT . TgA,CgT CgA,TgT
16230190 .... TgC,GgT GgT,TgC TgT,GgC TgC,GgT
输出:
d_mm = (d2_list+d2_list.shift(1)).dropna(how='all').\
applymap((lambda c: ','.join('g'.join(t) for t in it.product(*c) if '/' not in t if '/' in ''.join(map(str, c)) elif '|' in ''.join(map(str, c)) 'g'.join(t) for t in zip(*c) if '|' not in t if '|')))
and tried to put `elif` and `if ... not` in several different ways, but couldn't solve this nested loop problem.
S1 Sk1 S2 Sj
pos
2 16230174 .... C|T T|C T|C C|T
2 16230190 .... T|G G|T T|G T|G
16230174 .... CgA,TgT . TgA,CgT CgA,TgT
16230190 .... TgC,GgT GgT,TgC TgT,GgC TgC,GgT
谢谢,@Boud:谢谢,是的,我没有删除它。行。而且,我想做的与前面的几个问题并不完全相同,只是有些不同。不同问题的答案是不同的,我正在尝试将它们合并。在上一个问题中,我只需要得到
itertools.product
,但在这个问题中itertools.product(*c)
和zip(*c)
使用c中的值和c中的条件依赖项。问题在于试图将这两个条件放在同一行代码上。我在修复此嵌套循环时遇到问题。迟早您必须告诉我们使用c
和正确定义的命名函数有什么困难:P@AndrasDeak:你看了我为d_mm试过的代码了吗?我想类似的东西会有用的。我尝试过将条件1和条件2放在一起,但不断出现错误。如果这两个条件可以放在同一行中,那么添加第三个条件就不难了。请加入我,我想弄清这个lambda难题的真相。我甚至不确定我是否理解您的输入,所以我们应该在聊天中澄清这一点。