logo
down
shadow

how to create a dataframe form a lists within a list in R


how to create a dataframe form a lists within a list in R

By : user2953848
Date : November 21 2020, 07:31 AM
Hope this helps is there a chance to get a dataframe from a list that contains further lists? my data looks like this: , You could use melt/dcast from reshape2
code :
library(reshape2)
dcast(melt(lapply(l1, function(x) as.data.frame.list(x))),
                          variable~paste0('Var',L1), value.var='value')
#     variable Var1 Var2 Var3
#1        X7    3   NA    8
#2        X9    2   NA   10
#3        X1    6    9   NA
#4        X5    9    2    9
#5        X2    3   NA   NA
#6        X3    5    8    5
#7        X6    2    7   10
#8       X10   NA   10    5
#9        X4   NA    2    4
#10       X8   NA   NA   10
 dcast(melt(lapply(l1, as.matrix)), Var1 ~ L1, value.var = "value")
 library(data.table)
 rbindlist(lapply(l1, function(x) as.data.frame.list(x)), fill=TRUE)
# X7 X9 X1 X5 X2 X3 X6 X10 X4 X8
#1:  3  2  6  9  3  5  2  NA NA NA
#2: NA NA  9  2 NA  8  7  10  2 NA
#3:  8 10 NA  9 NA  5 10   5  4 10
set.seed(49)
l1 <- lapply(1:3 ,function(i) {x1 <- sample(1:10,sample(5:10),replace=TRUE)
         names(x1) <- sample(1:10, length(x1),replace=FALSE)
             x1})


Share : facebook icon twitter icon
Create dataframe from list of lists in R

Create dataframe from list of lists in R


By : Aruna Kumar B.S
Date : March 29 2020, 07:55 AM
With these it helps I have a variable out that is a list of lists and I want to format the first child list to a dataframe. Say my out looks like this: , try
code :
library(plyr)
ldply(out, as.data.frame)
How to create a list of lists of integers from DataFrame?

How to create a list of lists of integers from DataFrame?


By : Texo Gaming
Date : March 29 2020, 07:55 AM
wish helps you It seems they are stored as a string. Try the following (not very robust, but depending on your context it can be ok):
code :
slist = df[['Values']].values.tolist()
ilist = [ [int(s) for s in l[0].split(',')] for l in slist] 
Create a new pandas dataframe from a python list of lists with a column with lists

Create a new pandas dataframe from a python list of lists with a column with lists


By : Iris
Date : March 29 2020, 07:55 AM
this one helps. I have a python list of lists, e.g. [["chunky","bacon","foxes"],["dr_cham"],["organ","instructor"],...] and would like to create a pandas dataframe with one column containing the lists: , The following code should achieve what you want:
code :
import pandas as pd

l = [["hello", "goodbye"], ["cat", "dog"]]

# Replace "lists" with whatever you want to name the column
df = pd.DataFrame({"lists": l})
              lists
0  [hello, goodbye]
1        [cat, dog]
How to create a Pandas DataFrame from a list of lists?

How to create a Pandas DataFrame from a list of lists?


By : user2641094
Date : March 29 2020, 07:55 AM
it fixes the issue Use pandas.DataFrame.from_records:
code :
import pandas as pd

df = pd.DataFrame.from_records(x[1:]))
print(df)
    0                 1   2      3     4     5     6   7     8  9
0    1   Anthony Taylor   6  11.17  0.68  0.17  1.83  11  0.17  1
1    2    Jonathan Moss   4  12.75  0.73  0.00  2.00   8  0.00  0
2    3  Martin Atkinson   3  11.00  0.69  0.67  1.67   5  0.00  0
3    4     Graham Scott   3  10.00  0.67  0.00  0.33   1  0.00  0
4    5   Chris Kavanagh   3  10.33  0.74  0.00  1.67   5  0.00  0
5    6   Michael Oliver   2  12.00  0.62  1.00  2.00   4  0.00  0
6    7     Paul Tierney   2  12.50  0.66  0.00  2.50   5  0.00  0
7    8        Mike Dean   2  14.50  0.73  0.50  3.00   6  0.00  0
8    9   Andre Marriner   2   8.50  0.53  0.00  2.00   4  0.00  0
9   10     Kevin Friend   2   9.50  0.50  0.00  3.00   6  0.00  0
10  11     Craig Pawson   1   9.00  0.45  0.00  1.00   1  0.00  0
11  12   Stuart Attwell   1   9.00  0.75  0.00  2.00   2  0.00  0
12  13      Lee Probert   1  11.00  0.73  0.00  0.00   0  0.00  0
13      Total / Average  32  11.09  0.66  0.19  1.81  58  0.03  1
How to create a pandas dataframe from two lists with each value of first list having each value of second list as record

How to create a pandas dataframe from two lists with each value of first list having each value of second list as record


By : user2751567
Date : March 29 2020, 07:55 AM
Does that help Use list comprehension with the pandas.DataFrame constructor:
code :
df = pd.DataFrame([(i, s) for i in id for s in st], columns=['id', 'st'])
     id  st
0   243   1
1   243   3
2   243   5
3   243   9
4   243   2
5   243   6
6   243   7
7  2352   1
8  2352   3
9  2352   5
...
25   84   2
26   84   6
27   84   7
28  443   1
29  443   3
30  443   5
31  443   9
32  443   2
33  443   6
34  443   7
Related Posts Related Posts :
  • How do you combine two columns into a new column in a dataframe made of two or more different csv files?
  • Twitter authentication fails
  • Summing Values of One Vector Conditional on Values of Another Vector
  • draw cube into 3D scatterplot in RGL
  • lme4 translate formula to code in 3-level model
  • How to draw single axis plot in R
  • Combine geom_tile() and facet_grid/facet_wrap and remove space between tiles (ggplot2)
  • Use snpStats with R version 3.0.1
  • Makefile gives strange error while compiling markdown file into .docx file
  • How to determine whether a points lies in an ellipse
  • Summarize data already grouped in r
  • Is the bigvis package for R not available for R version 3.0.1?
  • Operator overloading in R reference classes
  • How to enable user to switch between ggplot2 and gVis graphs in R Shiny?
  • Is there an easy way to separate categorical vs continuous variables into two dataset in R
  • Correct previous year by id within R
  • Installation of rdyncall package for R
  • ggplot2 plot that evaluates the percentage and mean of a third variable at intersecting points
  • Error Handling with Lapply
  • data.table - split multiple columns
  • How to compute the overall mean for several files in R?
  • R: Graph Plotting: Subscripts in the legend like LaTeX
  • Restructuring data in R
  • Distance of pointsfrom cluster centers after K means clustering
  • R incorrect value of date function
  • Package "Imports" not loading in R development package
  • r - run a user defined function several times by taking column elements as parameters
  • Create input$selection to subset data AND radiobuttons to choose plot type in Shiny
  • Generate crosstabulations from dataframe of categorical variables in survey
  • Restructure output of R summary function
  • New behavior in data.table? .N / something with `by` (calculate proportion)
  • search certain number vector in R
  • R version doesn't support quartz graphic device - RStudio won't plot
  • Referencing a function parameter in R
  • How to synchronize signals using a cross-correlation and FFT in R?
  • Plotting coefficients and corresponding confidence intervals
  • passing expressions to curve() within a function
  • More effective merging of matched column with duplicates in data.table
  • Easy way to export multiple data.frame to multiple Excel worksheets
  • R Foreach Iterator - Walkforward
  • Table format and output in R
  • Restructuring data and duplicating rows in R
  • use ggplot2 to plot two lines with ribbons
  • how to plot a graph on lattice with two different colors
  • How can I keep a date formatted in R using sqldf?
  • Generating simulation data based on a specified distribution
  • Joining list of data frames in R
  • Subset data in R
  • R: How to avoid 2 'for' loops in R in this function
  • + signs appearing in console in R
  • Best way to combine and keep columns
  • Using identify and attach in a function
  • Apply function to each submatrix
  • How to assign regular strings for quarterly and monthly observation labels to the row names of a data frame?
  • Adjust hexbin legend breaks
  • Different lowess curves in plot and qplot in R
  • Extract words only with R
  • switch case: several equivalent cases expressions in r
  • R data.table to calculate a formula using a column as a variable across levels of a factor
  • how to create a line plot frame in ggplot2
  • shadow
    Privacy Policy - Terms - Contact Us © ourworld-yourmove.org