Save a Data Frame with List-Columns as CSV File

Save a data frame with list-columns as csv file

Create a tibble containing list columns:

library(tibble)

clinic_name <- c('bobo center', 'yoyo plaza', 'lolo market')
drop_in_hours <- list(c("Monday: 2 pm - 5 pm", "Tuesday: 4 pm - 7 pm"))
appointment_hours <- list(c("Monday: 1 pm - 2 pm", "Tuesday: 2 pm - 3 pm"))
services <- list(c("skin graft", "chicken heart replacement"))

tibb <- data_frame(clinic_name, drop_in_hours, appointment_hours, services)

print(tibb)

Sample Image

Write a general-purpose function that converts any list columns to character type:

set_lists_to_chars <- function(x) {
if(class(x) == 'list') {
y <- paste(unlist(x[1]), sep='', collapse=', ')
} else {
y <- x
}
return(y)
}

Apply function to tibble with list columns:

new_frame <- data.frame(lapply(tibb, set_lists_to_chars), stringsAsFactors = F)

new_frame

Sample Image

Write newly formatted dataframe as csv file:

write.csv(new_frame, file='Desktop/clinics.csv')

Sample Image

This is a csv file with the list columns expanded as regular strings.

Here is an all-encompassing function. Just pass in your tibble and a filename:

tibble_with_lists_to_csv <- function(tibble_object, file_path_name) {
set_lists_to_chars <- function(x) {
if(class(x) == 'list') { y <- paste(unlist(x[1]), sep='', collapse=', ') } else { y <- x }
return(y) }
new_frame <- data.frame(lapply(tibble_object, set_lists_to_chars), stringsAsFactors = F)
write.csv(new_frame, file=file_path_name)
}

Usage:

tibble_with_lists_to_csv(tibb, '~/Desktop/tibb.csv')

Save a data.frame of a list as a csv file in R

If we can create the data.frame after transposing the 'd', then it should also work

output <-  data.frame(t(d), dap = c(TRUE, FALSE))
row.names(output) <- NULL

The lapply in the second line of code is looping over column and then applying the as.list i.e. it creating a list of lists

lapply(d, as.list)
#$A
#$A[[1]]
#[1] 2

#$A[[2]]
#[1] 3

#$B
#$B[[1]]
#[1] 1

#$B[[2]]
#[1] 4

When we assign it to 'd' with keeping the same structure ([]), it would remain as a data.frame with list columns

d[] <- lapply(d, as.list) 
str(d)
#'data.frame': 2 obs. of 2 variables:
# $ A:List of 2 # each column is a `list`
# ..$ : num 2
# ..$ : num 3
# $ B:List of 2
# ..$ : num 1
# ..$ : num 4

Now, we are doing the rbind

rbind(d, dap = c(T, F))
# A B
#kap 2 1
#sap 3 4
#dap TRUE FALSE

and its transpose,

output <-  t(rbind(d, dap = c(T, F)))
output
# kap sap dap
#A 2 3 TRUE
#B 1 4 FALSE

which may look like a regular data.frame output, but it is not

str(output)
#List of 6
# $ : num 2
# $ : num 1
# $ : num 3
# $ : num 4
# $ : logi TRUE
# $ : logi FALSE
# - attr(*, "dim")= int [1:2] 2 3
# - attr(*, "dimnames")=List of 2
# ..$ : chr [1:2] "A" "B"
# ..$ : chr [1:3] "kap" "sap" "dap"

which is again wrappedd with data.frame

str(data.frame(output))
#'data.frame': 2 obs. of 3 variables:
# $ kap:List of 2
# ..$ A: num 2
# ..$ B: num 1
# $ sap:List of 2
# ..$ A: num 3
# ..$ B: num 4
# $ dap:List of 2
# ..$ A: logi TRUE
# ..$ B: logi FALSE

still a data.frame with list columns. One option is to loop over the list, unlist the list columns and wrap with data.frame

data

d <- data.frame(list(A = c(kap = 2, sap = 3), B = c(kap = 1, sap = 4)))

How to save data column wise in csv files from multiple list of list

first convert lists to numpy arrays

listA = np.array([[[1, 2, 3], [4, 5, 6], [7, 8, 9]], [[10, 20, 30], [40, 50, 60], [70, 80, 90]]])
listB = np.array([[[10, 20, 30], [40, 50, 60], [70, 80, 90]] , [[1, 2, 3], [4, 5, 6], [7, 8, 9]]])

next mix hetro-geniously all the arrays

a=np.vstack(np.vstack(np.stack((listA,listB),axis=2)))

output:

array([[ 1,  2,  3],
[10, 20, 30],
[ 4, 5, 6],
[40, 50, 60],
[ 7, 8, 9],
[70, 80, 90],
[10, 20, 30],
[ 1, 2, 3],
[40, 50, 60],
[ 4, 5, 6],
[70, 80, 90],
[ 7, 8, 9]])

Form a Dataframe:

pd.DataFrame(a).T

output:

    0   1   2   3   4   5   6   7   8   9   10  11
0 1 10 4 40 7 70 10 1 40 4 70 7
1 2 20 5 50 8 80 20 2 50 5 80 8
2 3 30 6 60 9 90 30 3 60 6 90 9

you just need to name the columns and use to_csv to get a csv.

in your case just re place this line and rest will work as you expected.

a=np.vstack(np.vstack(np.stack((listA,listB,listC),axis=2)))

How to save a dataframe with list to csv file in R

Since it's not a data.frame , and more specifically cannot be coerced to one with as.data.frame which is where that message came from, you need to think of a different method of saving the data. Probably this simplest would be with dput, which writes an ASCII representation of the list structure:

dput(operacions, file="out.txt")

To bring it back into R:

new <- source("out.txt")

Another method would be to convert to JSON format, which would also preserve the key-value information rather than just writing the values:

library(jsonlite)
toJSON(new)
# value---------
{"value":[{"Nom":["Victor"],"Bolis":["Negro","azul","verde"]},{"Nom":["Dani"],"Lapices":[1,2,3,4]}],"visible":[true]}

You can use the cat function to direct this to a text file:

cat( toJSON(new), file="test.3.txt")

Write a data frame containing a list to csv file

train.user$age <- unlist(train.user$age)

Technically, a data.frame is a list of equal-length vectors, but most functions will assume that all of the columns are atomic vectors and will fail when you try to use a list.

NB: Don't edit an answer into your question.

How to save a list as a CSV file with new lines?

use pandas to_csv (http://pandas.pydata.org/pandas-docs/dev/generated/pandas.DataFrame.to_csv.html)

>>> import pandas as pd
>>> df = pd.DataFrame(some_list, columns=["colummn"])
>>> df.to_csv('list.csv', index=False)

Saving each DataFrame column to separate CSV files

You can iterate through columns and write it to files.

for column in df.columns:
df[column].to_csv(column + '.csv')

Note: Assuming language to be python as the question has pd mentioned in it and all mentioned code is part of pandas



Related Topics



Leave a reply



Submit