Combine Two Lists of Dataframes, Dataframe by Dataframe

Take multiple lists into dataframe

I think you're almost there, try removing the extra square brackets around the lst's (Also you don't need to specify the column names when you're creating a dataframe from a dict like this):

import pandas as pd
lst1 = range(100)
lst2 = range(100)
lst3 = range(100)
percentile_list = pd.DataFrame(
{'lst1Title': lst1,
'lst2Title': lst2,
'lst3Title': lst3
})

percentile_list
lst1Title lst2Title lst3Title
0 0 0 0
1 1 1 1
2 2 2 2
3 3 3 3
4 4 4 4
5 5 5 5
6 6 6 6
...

If you need a more performant solution you can use np.column_stack rather than zip as in your first attempt, this has around a 2x speedup on the example here, however comes at bit of a cost of readability in my opinion:

import numpy as np
percentile_list = pd.DataFrame(np.column_stack([lst1, lst2, lst3]),
columns=['lst1Title', 'lst2Title', 'lst3Title'])

Merge two lists of dataframes

You could loop through both lists simultaneously and join each element using map2 from package purrr. To return a single data.frame rather than a list of separate, joined data.frames you can use map2_df.

library(purrr)
library(dplyr)

map2_df(list1, list2, inner_join, by = "Wvlgth")

Wvlgth Global group time IRD
1 337.0 .9923+00 0 13.445 0.01324
2 337.5 .01245+00 0 13.445 0.34565
3 338.0 .0005+00 0 13.445 0.92395
4 339.0 .74361+00 0 13.445 0.67489
5 337.0 .1284+00 1 13.45361 0.20981
6 337.5 .0098+00 1 13.45361 0.98703
7 338.0 .7853+00 1 13.45361 0.54092
8 339.0 .1211+00 1 13.45361 0.38567

Combine two lists of dataframes, dataframe by dataframe

> mapply(cbind, opr, timestamp, SIMPLIFY=FALSE)
$Cycling1.opr
V1 V2 timestamp
1 1 21 1
2 2 22 2
3 3 23 3
4 4 24 4
5 5 25 5

$Cycling2.opr
V1 V2 timestamp
1 1 21 1
2 2 22 2
3 3 23 3
4 4 24 4
5 5 25 5
6 6 26 6
7 7 27 7
8 8 28 8
9 9 29 9
10 10 30 10

$Cycling3.opr
V1 V2 timestamp
1 1 21 1
2 2 22 2
3 3 23 3
4 4 24 4
5 5 25 5
6 6 26 6
7 7 27 7
8 8 28 8
9 9 29 9
10 10 30 10
11 11 31 11
12 12 32 12
13 13 33 13
14 14 34 14
15 15 35 15
16 16 36 16
17 17 37 17
18 18 38 18
19 19 39 19
20 20 40 20
21 21 41 21
22 22 42 22
23 23 43 23
24 24 44 24
25 25 45 25
26 26 46 26
27 27 47 27
28 28 48 28
29 29 49 29
30 30 50 30

Here's the structure:

> str(mapply(cbind, opr, timestamp, SIMPLIFY=FALSE))
List of 3
$ Cycling1.opr:'data.frame': 5 obs. of 3 variables:
..$ V1 : int [1:5] 1 2 3 4 5
..$ V2 : int [1:5] 21 22 23 24 25
..$ timestamp: int [1:5] 1 2 3 4 5
$ Cycling2.opr:'data.frame': 10 obs. of 3 variables:
..$ V1 : int [1:10] 1 2 3 4 5 6 7 8 9 10
..$ V2 : int [1:10] 21 22 23 24 25 26 27 28 29 30
..$ timestamp: int [1:10] 1 2 3 4 5 6 7 8 9 10
$ Cycling3.opr:'data.frame': 30 obs. of 3 variables:
..$ V1 : int [1:30] 1 2 3 4 5 6 7 8 9 10 ...
..$ V2 : int [1:30] 21 22 23 24 25 26 27 28 29 30 ...
..$ timestamp: int [1:30] 1 2 3 4 5 6 7 8 9 10 ...

combining data frames from two lists

It looks like this is what you want:

map2(x, y, ~ inner_join(.x, .y))
[[1]]
i x z x1 z1
1 1 0.7715183 -0.6933826 -0.3335239 0.5957587
2 2 -0.3824746 -0.7248827 -1.6736241 -1.2248904
3 3 0.3412777 -0.3711940 0.9334678 0.4043867
4 4 -0.4225862 -1.6653314 1.0369985 1.1808140
5 5 0.7468157 0.1704126 -0.1470796 -1.6237296

[[2]]
i x z x1 z1
1 1 0.69264103 -0.6640663 -0.2253319 0.26323254
2 2 -0.07861775 0.7914119 0.3725911 0.02854667
3 3 -0.86588724 -0.5519633 -1.5114177 -0.14283509
4 4 1.16069947 1.1299540 -0.4207173 -1.15829758
5 5 2.13867104 -0.9668079 0.1082068 -2.74714297

Merging list of Dataframes depending on another list of lists

You don't need outer for:

dfs_list=[]

for nums in list_num:
dfs_list.append(pd.concat([dfs[i] for i in nums], join="inner"))

Concatenate a list of pandas dataframes together

Given that all the dataframes have the same columns, you can simply concat them:

import pandas as pd
df = pd.concat(list_of_dataframes)

Combine dataframes in two different lists keyed on the element name in R

You can try using the unique names from list1 and list2 as you have already tried and then use them to setnames:

keys <- unique(c(names(list1), names(list2)))
x <- setNames(Map(rbind, list1[keys], list2[keys]), keys)
identical(x, combined_list)
#[1] TRUE

or using lapply:

x <- lapply(setNames(keys, keys), function(x) {rbind(list1[[x]], list2[[x]])})
identical(x, combined_list)
#[1] TRUE

R: merge two lists of lists of dataframes

Here is a solution using base R:

x <- c(L1, L2)
lapply(split(x, names(x)), function(i){
xsub <- do.call(c, unname(i))
lapply(split(xsub, names(xsub)), function(j) do.call(rbind, unname(j)))
})
  • split(x, names(x)) will put Q1s together and Q2s together;
  • xsub <- do.call(c, unname(i)) will combine Q1s or Q2s into a list data.frames;
  • split(xsub, names(xsub)) will group data.frames by their names (A, B, C);

The output is:

# $Q1
# $Q1$A
# X1
# 1 1
# 2 2
# 3 3
#
# $Q1$B
# X1
# 1 4
# 2 5
# 3 6
#
# $Q1$C
# X1
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 6
#
#
# $Q2
# $Q2$A
# X1
# 1 4
# 2 5
# 3 6
#
# $Q2$B
# X1
# 1 1
# 2 2
# 3 3
#
# $Q2$C
# X1
# 1 1
# 2 2
# 3 3
# 4 4
# 5 5
# 6 6

join two lists of data frames into a single list of binded_rows data frames

We need map2 for binding two corresponding lists

library(purrr)
map2_dfr(rapheys_df_list, XGB_models_Prep, bind_rows)

data

rapheys_df_list <- list(data.frame(col1 = 1:3, col2 = 4:6), 
data.frame(col1 = 7:9, col2 = 10:12))
XGB_models_Prep <- list(data.frame(col1 = 2:5, col2 = 3:6),
data.frame(col1 = 4:6, col2 = 0:2))

how to combine two dataframes in two different lists having same names into a single list with dataframes using R

the problem is with the structure of the two dataframes are different and the problem is solved by converting the structure of two lists into the same structure.

i have converted the structure of list 'b' into tibble as

b <- tibble::as_tibble(b)

then we can use the code as following

c <-  Map(cbind, a, b)

this has worked solved my problem



Related Topics



Leave a reply



Submit