从一个更大的数据框中创建唯一命名的数据框

时间:2019-03-25 16:26:06

标签: python r pandas dataframe

是处理数据帧和循环的新手。在python或R中寻找查询的答案。我有一个数据框,其结构与以下类似。

        TP1.v1  | TP1.v2 | TP1.v3 | TP2.v1 | TP2.v2 | TP2.v3 |... TPn.v1
 Gene A|  7     |6       |7       |6       |4       |1       |... 9    
 Gene B|  3     |4       |4       |4       |5       |3       |... 3    
 Gene n|  6     |1       |1       |5       |7       |7       |... 8     

我想为所有TP1,TP2等创建一个新的数据帧。每个TP(时间点)具有3个与之关联的列。理想情况下,我还想使用循环来执行此操作,因为我有多个具有相似结构的文件。最后,我希望循环为每个新数据帧赋予一个新的唯一名称。

我已经能够在R中完成此任务而无需使用循环。简单地重复使用基本功能来操纵数据框。但这很慢且很费力,因此想循环执行。

理想的输出将是n个唯一命名的数据帧,每个数据帧具有3列,并且每个字段保留原始数据帧中的行名和列名。

下面,我添加了R中dput(head(df))的输出。

structure(list(D1.log2fc = c(-0.453086, -0.1828075, 0.105551500000001, 
0.368134000000001, 0.194800000000001, -0.327664499999999), D1.AveExp = c(4.9001385, 
5.59887075, 9.35607416666667, 9.466082, 9.28132575, 5.43070783333333    
), D1.adjPval = c(0.158162310733078, 0.680539779380169, 0.798318133631351, 
0.368809197240543, 0.588741274410125, 0.363696882398466), D3.log2fc = c(-0.5979695, 
-0.510921500000001, 0.544158999999999, 0.354766, 0.631701999999999, 
-0.365363499999998), D3.AveExp = c(4.9001385, 5.59887075, 9.35607416666667, 
9.466082, 9.28132575, 5.43070783333333), D3.adjPval =  c(0.0354796268783931, 
0.104426887750224, 0.0342979093938487, 0.318289098430963, 0.0318404713171763, 
0.231275103023615), D6.log2fc = c(-0.349413, -0.854375500000001, 
0.7416965, 0.5901225, 0.821465500000002, -0.578061499999999), 
D6.AveExp = c(4.9001385, 5.59887075, 9.35607416666667, 9.466082, 
9.28132575, 5.43070783333333), D6.adjPval = c(0.151181193217808, 
0.00788722811936, 0.00487109163210043, 0.0635131764099792, 
0.00547087529420614, 0.0423872835135151), D10.log2fc =      c(-0.528707499999999, 
-0.431807000000002, 0.454508000000001, 0.628860999999999, 
0.379918500000002, -0.195571999999999), D10.AveExp = c(4.9001385, 
5.59887075, 9.35607416666667, 9.466082, 9.28132575, 5.43070783333333
), D10.adjPval = c(0.0360033103086792, 0.125511404231851, 
0.0445352483558512, 0.0499786423872913, 0.126969394135026, 
0.517590415583245), D14.log2fc = c(-0.517372, -0.379950000000001, 
0.596869, 0.7255935, 0.6545535, -0.205755499999999), D14.AveExp = c(4.9001385, 
5.59887075, 9.35607416666667, 9.466082, 9.28132575, 5.43070783333333
), D14.adjPval = c(0.039311630129941, 0.172677856404577, 
0.0124695746689562, 0.0265985268105264, 0.0152333310246979, 
0.452405710914221)), row.names = c("hsa-let-7a-2", "hsa-let-7b", 
"hsa-let-7d", "hsa-let-7e", "hsa-let-7f", "hsa-let-7f1"), class = "data.frame")

2 个答案:

答案 0 :(得分:1)

不确定唯一命名的DataFrames是什么意思。这将创建一个包含每个DataFrame的字典。希望对您有所帮助。

import pandas as pd
import numpy as np

# Sample Data
df = pd.DataFrame(np.random.rand(50,3*10), 
                  columns = ['TP%d.v%d'%(i, j) for i in range(1,11) for j in range(1,4)])

# Construct dictionary:
dd = {}
for name in df.columns.str.split('.').str[0].unique():
    dd[name] = df[df.columns[df.columns.str.startswith(name)]].copy()

如果您想使用多索引DataFrames。以下解决方案将简单地重新定义当前DataFrame的列。处理这些问题可能会更加复杂,但是效率更高:

# MultiIndex Solution
df.columns = df.columns.str.split('.', expand=True)

答案 1 :(得分:1)

R

中有几种方法可以做到这一点
# assuming you know the prefix and how many time points you have (e.g. D and 5)
tp <- c(1, 3, 6, 10, 14)
prefix <- "D"

# for loop
for (i in tp) {
  common <- paste0(prefix, i) # create common name e.g. D1, D3, D6 etc.
  # assign columns to its unique df
  assign(common, df[, grep(paste0(common, "\\."), colnames(df), ignore.case = T)])
}

# using lapply (could be a bit faster than for loop)
lapply(tp, function(i) {
  common <- paste0(prefix, i) # create common name e.g. D1, D3, D6 etc.
  # assign columns to its unique df
  assign(common, df[, grep(paste0(common, "\\."), colnames(df), ignore.case = T)], envir = .GlobalEnv)
})

编辑:lapply实际上比for循环快得多。这是microbenchmark个结果

Unit: microseconds
        expr      min       lq      mean    median       uq      max neval
    for.loop 3045.718 3167.800 3549.2943 3284.6260 3424.485 79971.27  1000
 lapply.call  170.647  184.086  204.4465  192.4345  200.538  4123.52  1000