我试图使用python pandas read_table函数从我的文件中读取一定范围的非连续列.为此,我正在努力:
df=pd.read_table('genes.fpkm_trackingTest', usecols=[0:4, 8,9, 12:19])
Run Code Online (Sandbox Code Playgroud)
我的想法是,我尝试使用":"来选择usecols的列数范围,而不是使用以逗号","分隔的列号.我收到语法错误.如果我使用逗号","分隔列号,那么它工作正常.
df=pd.read_table('genes.fpkm_trackingTest', usecols=[0,1,2,4, 8,9, 12,13,14,15,16,17,18,19])
Run Code Online (Sandbox Code Playgroud)
然而,这可能很麻烦,因为有时我必须选择40列.我怎么能绕过这个?
我甚至试过了
usecols=[range(0:4), 8, 9, range(12:19)]
Run Code Online (Sandbox Code Playgroud)
但它也给了我错误.
我认为这应该是简单的事情要解决,但我无法在网上找到解决方案.
我正在尝试编写一个脚本,通过某个模式/变量循环文件,然后它连接文件的第8列,同时保留所有文件通用的前4列.如果我使用以下命令,该脚本可以工作:
reader = csv.reader(open("1isoforms.fpkm_tracking.txt", 'rU'), delimiter='\t') #to read the header names so i can use them as index. all headers for the three files are the same
header_row = reader.next() # Gets the header
df1 = pd.read_csv("1isoforms.fpkm_tracking.txt", index_col=header_row[0:4], sep="\t") #file #1 with index as first 5 columns
df2 = pd.read_csv("2isoforms.fpkm_tracking.txt", index_col=header_row[0:4], sep="\t") #file #2 with index as first 5 columns
df3 = pd.read_csv("3isoforms.fpkm_tracking.txt", index_col=header_row[0:4], sep="\t") #file #3 with index as first 5 columns
result = pd.concat([df1.ix[:,4], df2.ix[:,4]], keys=["Header1", "Header2", "Header3"], …Run Code Online (Sandbox Code Playgroud) 我的任务是生成用于计算 Z 分数的 R 代码,然后将其输出到文件。虽然脚本有效,但我对一些令我困惑的台词有一些疑问。输入.txt:
基因 ID GeneID-2 基因名称 TSS-ID 基因座 ID 样本 1 样本 2 样本 3 样本 4 样本 5 ID1 X1 Zranb2 TSS1 Loc1 22.49161667 14.7231 19.62885833 26.16171667 39.3109 ID2 X2 Lphn2 TSS2 Loc2 6.439735 5.920786667 8.883331667 7.696353333 10.46969333 ID3 X3 Rpf1 TSS3 Loc3 30.67975 20.93751667 27.30251667 31.55653333 58.57418333 ID4 X4 Ctbs TSS4 Loc4 1.916071667 1.943611667 2.696701667 3.130295 2.74012 ID5 X5 Spata1 TSS5 Loc5 0.715265667 0.3318745 0.4183155 0.961065833 1.10731 ID6 X6 Sap30bp TSS6 Loc6 21.65946667 23.84386667 28.39683333 …
我想使用 groupby 但保留所有列来计算数字列的平均值。来自 7 列的数据框示例如下:
tracking_id 基因_id 基因_short_name tss_id 基因座 FPKM-1 FPKM-2 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 Tss1231 1:4490927-4496413 0.611985 232 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 Ts412 1:4490927-4496413 12 21 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 Ts56 1:4490927-4496413 2 213 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 TS512 1:4490927-4496413 0.611985 5 ENSMUSG00000025902 ENSMUSG00000025902 Sox17 TS12241 1:4490927-4496413 0.611985 51 ENSMUSG00000096126 ENSMUSG00000096126 Gm22307 TS124 1:4529016-4529123 35 1 ENSMUSG00000096126 ENSMUSG00000096126 Gm22307 TS-1824 1:4529016-4529123 1 2 ENSMUSG00000096126 ENSMUSG00000096126 Gm22307 TS1249082 1:4529016-4529123 2 5 ENSMUSG00000088000 ENSMUSG00000088000 Gm25493 TS1290328 1:4723276-4723379 0 1 ENSMUSG00000098104 ENSMUSG00000098104 Gm6085 TS01239-1 1:4687933-4689403 …
我正在尝试生成相关矩阵的表输出。具体来说,我使用 for 循环来识别第 4:40 列与第 1 列中的所有数据之间的相关性。虽然该表的结果不错,但它无法识别正在与什么进行比较。在检查 的属性时cor.test,我发现 data.name 被指定为x[1]和y[1],这不足以追踪哪些列正在与哪些列进行比较。这是我的代码:
input <- read.delim(file="InputData.txt", header=TRUE)
x<-input[,41, drop=FALSE]
y=input[,4:40]
corr.values <- vector("list", 37)
for (i in 1:length(y) ){
corr.values[[i]] <- cor.test(x[[1]], y[[i]], method="pearson")
}
lres <- sapply(corr.values, `[`, c("statistic","p.value","estimate","method", "data.name"))
lres<-t(lres)
write.table(lres, file="output.xls", sep="\t",row.names=TRUE)
Run Code Online (Sandbox Code Playgroud)
输出文件如下所示:
statistic p.value estimate method data.name
1 -2.030111981 0.042938137 -0.095687495 Pearson's product-moment correlation x[[1]] and y[[i]]
2 -2.795786248 0.005400938 -0.131239287 Pearson's product-moment correlation x[[1]] and y[[i]]
3 -2.099114632 0.036368337 -0.098908573 …Run Code Online (Sandbox Code Playgroud) pandas ×3
python ×3
dataframe ×2
r ×2
correlation ×1
group-by ×1
loops ×1
optimization ×1
statistics ×1