【问题标题】:join files based on one column and different orders in R or python在 R 或 python 中基于一列和不同的顺序连接文件
【发布时间】:2020-07-20 20:53:36
【问题描述】:

我有 3 个制表符分隔文件,如下 3 个示例:

示例文件:

AB  45.2    4.56    0.21
FG  78.1    54.1    36.1
HG  98.1    25.0    12.6
TR  1.2 3.25    65.1


TR  5.2 41.6    10.21
HG  8.1 23.1    56.1
FG  9   32.0    32.6
AB  12.2    31.25   5.1


HG  15.2    21.6    20.21
TR  31.1    32.1    66.1
AB  12.1    12.0    62.6
FG  11.3    31.25   54.1

所有这些中的第一列都有相似的项目,但顺序不同。我想根据第一列加入文件并制作一个新文件,如预期的输出:

预期输出

AB  45.2    4.56    0.21    12.2    31.25   5.1 12.1    12.0    62.6
FG  78.1    54.1    36.1    9   32.0    32.6    11.3    31.25   54.1
HG  98.1    25.0    12.6    8.1 23.1    56.1    15.2    21.6    20.21
TR  1.2 3.25    65.1    5.2 41.6    10.21   31.1    32.1    66.1

我尝试在R and pandas 中使用join 函数,但它们没有返回预期的输出。你知道我如何在 python 或 R 中做到这一点吗?

【问题讨论】:

  • 最好显示你的代码,看起来你需要pd.concat([df1,df2,df3],axis=1)
  • 加入前是否对列进行排序?
  • 它将沿着索引加入

标签: r pandas join


【解决方案1】:

在 R 中,您可以使用 Reducemerge

Reduce(function(x, y) merge(x, y, by = 'V1'), list(df1, df2, df3))
#If there are lot of dataframes use `mget` and `ls`
#Reduce(function(x, y) merge(x, y, by = 'V1'), mget(ls(pattern = "df\\d+")))

#  V1   V2    V3    V4   V5   V6   V7   V8   V9  V10
#1 AB 45.2  4.56  0.21 12.2 31.2  5.1 12.1 12.0 62.6
#2 FG 78.1 54.10 36.10  9.0 32.0 32.6 11.3 31.2 54.1
#3 HG 98.1 25.00 12.60  8.1 23.1 56.1 15.2 21.6 20.2
#4 TR  1.2  3.25 65.10  5.2 41.6 10.2 31.1 32.1 66.1

数据

其中数据中的一列在所有数据帧中是通用的,其余列具有不同的名称。

df1 <- structure(list(V1 = structure(1:4, .Label = c("AB", "FG", "HG", 
"TR"), class = "factor"), V2 = c(45.2, 78.1, 98.1, 1.2), V3 = c(4.56, 
54.1, 25, 3.25), V4 = c(0.21, 36.1, 12.6, 65.1)), 
class = "data.frame", row.names = c(NA, -4L))

df2 <- structure(list(V1 = structure(4:1, .Label = c("AB", "FG", "HG", 
"TR"), class = "factor"), V5 = c(5.2, 8.1, 9, 12.2), V6 = c(41.6, 
23.1, 32, 31.25), V7 = c(10.21, 56.1, 32.6, 5.1)), class = "data.frame", 
row.names = c(NA, -4L))

df3 <- structure(list(V1 = structure(c(3L, 4L, 1L, 2L), .Label = c("AB", 
"FG", "HG", "TR"), class = "factor"), V8 = c(15.2, 31.1, 12.1, 
11.3), V9 = c(21.6, 32.1, 12, 31.25), V10 = c(20.21, 66.1, 62.6, 
54.1)), class = "data.frame", row.names = c(NA, -4L))

【讨论】:

    【解决方案2】:

    我们可以在tidyverse 中使用reduceinner_join(在R 中)

    library(dplyr)
    library(purrr)
    mget(paste0('df', 1:3)) %>%
       reduce(inner_join)
    

    数据

    df1 <- structure(list(V1 = structure(1:4, .Label = c("AB", "FG", "HG", 
    "TR"), class = "factor"), V2 = c(45.2, 78.1, 98.1, 1.2), V3 = c(4.56, 
    54.1, 25, 3.25), V4 = c(0.21, 36.1, 12.6, 65.1)), 
    class = "data.frame", row.names = c(NA, -4L))
    
    df2 <- structure(list(V1 = structure(4:1, .Label = c("AB", "FG", "HG", 
    "TR"), class = "factor"), V5 = c(5.2, 8.1, 9, 12.2), V6 = c(41.6, 
    23.1, 32, 31.25), V7 = c(10.21, 56.1, 32.6, 5.1)), class = "data.frame", 
    row.names = c(NA, -4L))
    
    df3 <- structure(list(V1 = structure(c(3L, 4L, 1L, 2L), .Label = c("AB", 
    "FG", "HG", "TR"), class = "factor"), V8 = c(15.2, 31.1, 12.1, 
    11.3), V9 = c(21.6, 32.1, 12, 31.25), V10 = c(20.21, 66.1, 62.6, 
    54.1)), class = "data.frame", row.names = c(NA, -4L))
    

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-10-05
      • 1970-01-01
      • 1970-01-01
      • 2019-02-04
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多