Python3-循环行,然后循环一些列以打印文本
我有一个熊猫数据框,看起来像这样:Python3-循环行,然后循环一些列以打印文本,python,pandas,Python,Pandas,我有一个熊猫数据框,看起来像这样: 0 1 2 3 \ 0 UICEX_0001 path/to/bam_T.bam path/to/bam_N.bam chr1:10000 1 UICEX_0002 path/to/bam_T2.bam path/to/bam_N2.bam chr54:4958392 4
0 1 2 3 \
0 UICEX_0001 path/to/bam_T.bam path/to/bam_N.bam chr1:10000
1 UICEX_0002 path/to/bam_T2.bam path/to/bam_N2.bam chr54:4958392
4
0 chr4:4958392
1 NaN
我试图循环遍历每一行并打印文本以输出给另一个程序。我需要打印前三列(以及其他一些文本),然后浏览其余的列并根据它们是否为NaN打印不同的内容
这主要起作用:
当前代码
def CreateIGVBatchScript(x):
for row in x.iterrows():
print("\nnew")
sample = x[0]
bamt = x[1]
bamn = x[2]
print("\nload", bamt.to_string(index=False), "\nload", bamn.to_string(index=False))
for col in range(3, len(x.columns)):
position = x[col]
if position.isnull().values.any():
print("\n")
else:
position = position.to_string(index=False)
print("\ngoto ", position, "\ncollapse\nsnapshot ", sample.to_string(index=False), "_", position,".png\n")
CreateIGVBatchScript(data)
但输出如下所示:
0 1 2 3 \
0 UICEX_0001 path/to/bam_T.bam path/to/bam_N.bam chr1:10000
1 UICEX_0002 path/to/bam_T2.bam path/to/bam_N2.bam chr54:4958392
4
0 chr4:4958392
1 NaN
实际产出
new
load path/to/bam_T.bam
path/to/bam_T2.bam
load path/to/bam_N.bam
path/to/bam_N2.bam
goto chr1:10000
chr54:4958392
collapse
snapshot UICEX_0001 **<-- ISSUE: it's printing both rows at the same time**
UICEX_0002 _ chr1:10000
chr54:4958392 .png
new
load path/to/bam_T.bam
path/to/bam_T2.bam
load path/to/bam_N.bam
path/to/bam_N2.bam
goto chr1:10000
chr54:4958392
collapse
snapshot UICEX_0001 **<-- ISSUE: it's printing both rows at the same time**
UICEX_0002 _ chr1:10000
chr54:4958392 .png
goto chr1:10000
collapse
snapshot UICEX_0001_chr1:10000.png
goto chr54:4958392
collapse
snapshot UICEX_0001_chr54:495832.png
额外信息
顺便说一句,为了更好地学习Python,我正试图从一个R脚本中修改它。以下是R代码,以防有帮助:
CreateIGVBatchScript <- function(x){
for(i in 1:nrow(x)){
cat("\nnew")
sample = as.character(x[i, 1])
bamt = as.character(x[i, 2])
bamn = as.character(x[i, 3])
cat("\nload",bamt,"\nload",bamn)
for(j in 4:ncol(x)){
if(x[i, j] == "" | is.na(x[i, j])){ cat("\n") }
else{
cat("\ngoto ", as.character(x[i, j]),"\ncollapse\nsnapshot ", sample, "_", x[i,j],".png\n", sep = "")
}
}
}
cat("\nexit")
}
CreateIGVBatchScript(data)
CreateIGVBatchScript我已经找到了答案。这里有几个问题:
我错误地使用了iterrows()
iterrows对象实际上保存了行中的信息,然后您可以使用索引保存该序列中的值
for index, row in x.iterrows():
sample = row[0]
将该行中的值保存在第0列中
在列上迭代
此时,您可以使用一个简单的for循环,就像我在迭代列时所做的那样
for col in range(3, len(data.columns)):
position = row[col]
用于保存该列中的值
最后的Python代码是:
def CreateIGVBatchScript(x):
x=x.fillna(value=999)
for index, row in x.iterrows():
print("\nnew", sep="")
sample = row[0]
bamt = row[1]
bamn = row[2]
print("\nload ", bamt, "\nload ", bamn, sep="")
for col in range(3, len(data.columns)):
position = row[col]
if position == 999:
print("\n")
else:
print("\ngoto ", position, "\ncollapse\nsnapshot ", sample, "_", position, ".png\n", sep="")
CreateIGVBatchScript(data)
答案由以下帖子指导:
我已经想出了答案。这里有几个问题:
- 我错误地使用了
iterrows()
iterrows对象实际上保存了行中的信息,然后您可以使用索引保存该序列中的值
for index, row in x.iterrows():
sample = row[0]
将该行中的值保存在第0列中
在列上迭代
此时,您可以使用一个简单的for循环,就像我在迭代列时所做的那样
for col in range(3, len(data.columns)):
position = row[col]
用于保存该列中的值
最后的Python代码是:
def CreateIGVBatchScript(x):
x=x.fillna(value=999)
for index, row in x.iterrows():
print("\nnew", sep="")
sample = row[0]
bamt = row[1]
bamn = row[2]
print("\nload ", bamt, "\nload ", bamn, sep="")
for col in range(3, len(data.columns)):
position = row[col]
if position == 999:
print("\n")
else:
print("\ngoto ", position, "\ncollapse\nsnapshot ", sample, "_", position, ".png\n", sep="")
CreateIGVBatchScript(data)
答案由以下帖子指导: