简体   繁体   English

如何从其他 2 个数据框填充数据框

[英]How to populate a data frame from 2 other data frames

I am trying to combine two dataframes in an specific way and creat a new dataframe, and avoid for loops.我试图以特定方式组合两个数据帧并创建一个新的数据帧,并避免循环。
Assume a dataframe 1 like,假设数据帧 1 像,

DF1  
    chan   
1    A01  
2    A02  
3    A03  
4    A04  

and dataframe 2 is:和数据框 2 是:

DF2  
     Len  
1     10  
2     11  
3     12

I need to creat the third dataframe that look like (without using for loops):我需要创建看起来像的第三个数据框(不使用 for 循环):

DF3  
     chan  Len  
1     A01   10  
2     A01   11  
3     A01   12  
1     A02   10  
2     A02   11  
3     A02   12

Appreciate answers in R and/or python.欣赏 R 和/或 python 中的答案。 Thanks a lot in advance!非常感谢!

阿德里安,试试

df3 <- expand.grid(df1$chan, df2$Len)

I think what you want to do is a cross join.我认为你想做的是交叉连接。 Several packages can help you do this like base R's merge function, join functions from the dplyr package, etc. I like to use the crossing function from the tidyr package.有几个包可以帮助你做到这一点,比如 base R 的merge函数、 dplyr包中的 join 函数等。我喜欢使用tidyr包中的crossing函数。

DF1 <- tibble(Chan = c("A01","A02","A03","A04"))
DF2 <- tibble(Len = 10:12)

tidyr::crossing(A, B)

# A tibble: 12 x 2
   A         B
   <chr> <int>
 1 A01      10
 2 A01      11
 3 A01      12
 4 A02      10
 5 A02      11
 6 A02      12
 7 A03      10
 8 A03      11
 9 A03      12
10 A04      10
11 A04      11
12 A04      12

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何匹配数据框之间的范围间隔和一个条件,并将一个数据框的值添加到另一个数据框 - How to match range intervals and one condition between data frames and add values from one to the other data frame 如何以最少的列值从其他数据框中唯一地定义一个数据框? - how to uniquely define a data frame from other data frames, with minimum number of column values? Pandas。 如何从数据框列表中调用数据框? - Pandas. How to call data frame from the list of data frames? 如何从三个数据帧上的 if 语句的结果创建数据帧 - How to create a data frame from the results of an if statement on three data frames 从 groupby 数据框创建数据框 - Creating data frames from a groupby data frame 匹配两个数据帧中的姓名和姓氏,从一个数据帧中提取中间名,并将 append 提取到另一个数据帧 - match name and surname from two data frames, extract middle name from one data frame and append it to other 根据其他两个数据帧填充新的数据帧 - Filling in a new data frame based on two other data frames 如何将来自许多数据帧的数据组合成一个数据帧,并将数组作为数据值 - How to combine the data from many data frames into a single data frame with an array as the data values 熊猫基于其他两个“子”框架创建数据框架 - Pandas create a data frame based on two other 'sub' frames Python:如何使用 2 个数据帧创建新的数据帧,这两个数据帧之一的数据必须被其他列名替换 - Python: How To create new data-frame with 2 data-frames that datas from one of those two have to be replaced by other's column name
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM