1
我有一個熊貓數據幀,看起來是這樣的:Python3 - 遍歷行,然後一些列打印文本
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)
但輸出看起來是這樣的:
實際輸出
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
的額外信息 順便說一句,我其實想從R腳本,以適應這更好地學習Python。這裏的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)