I have an example data frame:
df <- data.frame(x = 1:112, y = runif(112))
Is there a way to print a list of data frames with the first part of the list containing rows 1:10
, the second 11:20
, etc. up until the end (111:112
)?
You could use split() , with rep() to create the groupings. How will I write a code such that it iteratively saves each of the 10 chunks as a csv file each with a unique filename? The x and each arguments are flippled if the goal is to split the df into n parts.
DataFrame elements can be divided by a pandas series or by a Python sequence as well. Calling div() on a DataFrame instance is equivalent to invoking the division operator (/). The div() method provides the fill_value parameter which is used for replacing the np.
In the above example, the data frame 'df' is split into 2 parts 'df1' and 'df2' on the basis of values of column 'Weight'. Method 2: Using Dataframe. groupby(). This method is used to split the data into groups based on some criteria.
You can also do the following: split(x = df, f = ~ var1 + var2...) This way, you can also achieve the same split dataframe by many variables without using a list in the f parameter.
You could use split()
, with rep()
to create the groupings.
n <- 10 nr <- nrow(df) split(df, rep(1:ceiling(nr/n), each=n, length.out=nr))
This can be solved with nesting using tidyr/dplyr
require(dplyr) require(tidyr) num_groups = 10 iris %>% group_by((row_number()-1) %/% (n()/num_groups)) %>% nest %>% pull(data)
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