I have this nested data frame
test <- structure(list(id = c(13, 27), seq = structure(list(
`1` = c("1997", "1997", "1997", "2007"),
`2` = c("2007", "2007", "2007", "2007", "2007", "2007", "2007")),
.Names = c("1", "2"))), .Names = c("penr",
"seq"), row.names = c("1", "2"), class = "data.frame")
I want a list of all values in the second column, namely
result <- c("1997", "1997", "1997", "2007", "2007", "2007", "2007", "2007", "2007", "2007", "2007")
Is there an easy way to achieve this?
The first method to flatten the pandas dataframe is through NumPy python package. There is a function in NumPy that is numpy. flatten() that perform this task. First, you have to convert the dataframe to numpy using the to_numpy() method and then apply the flatten() method.
In this article, we are going to see how to flatten a list of DataFrames. Flattening is defined as Converting or Changing data format to a narrow format. The advantage of the flattened list is Increases the computing speed and Good understanding of data.
Index.flatten(order='C') Return a copy of the array collapsed into one dimension.
Flatten columns: use get_level_values() Flatten columns: use to_flat_index() Flatten columns: join column labels. Flatten rows: flatten all levels.
This line does the trick:
do.call("c", test[["seq"]])
or equivalent:
c(test[["seq"]], recursive = TRUE)
or even:
unlist(test[["seq"]])
The output of these functions is:
11 12 13 14 21 22 23 24 25 26 27
"1997" "1997" "1997" "2007" "2007" "2007" "2007" "2007" "2007" "2007" "2007"
To get rid of the names above the character vector, call as.character
on the resulting object:
> as.character((unlist(test[["seq"]])))
[1] "1997" "1997" "1997" "2007" "2007" "2007" "2007" "2007" "2007" "2007"
[11] "2007"
If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!
Donate Us With