python - Handle multiple values in column -


consider example data.table package in r:

dt = data.table(id = c("b","b","b","a","a","c"), = 1:6, b = 7:12, c=13:18) dt = dt[, .(a=list(a), b=list(b), c=list(c)), by=id] dt    id         b        c 1:  b 1,2,3 7,8,9 13,14,15 2:    4,5 10,11    16,17 3:  c     6    12       18 

after want write file in order share structure. however, prohibits writing such things write.csv because of list type. solution found convert columns string. however, how can read file? there unified format can read in (almost) language without effort?

you can create tab-seperated file follows:

dt2 <- dt[, .(a=tostring(a), b=tostring(b), c=tostring(c)), by=id] write.table(dt2, "dt2.txt", sep="\t", row.names = false) 

which should readable languages.


when want preserve lists, transforming json suggested @tigerhawkt3 best option:

dt3 <- dt[, .(a=list(a), b=list(b), c=list(c)), by=id] library(jsonlite) tojson(dt3) 

which gives:

[{"id":"b","a":[1,2,3],"b":[7,8,9],"c":[13,14,15]},{"id":"a","a":[4,5],"b":[10,11],"c":[16,17]},{"id":"c","a":[6],"b":[12],"c":[18]}] 

Comments

Popular posts from this blog

php - Wordpress website dashboard page or post editor content is not showing but front end data is showing properly -

python - Scipy.Odr multiple variable regression -

c# - Abstract method in public abstract class implementing an internal interface doesn't compile? -