Splits a Pandas DataFrame into Sub-Dataframes Based on Pattern

To split one dataframe into list of dataframes based on the pattern, use the split function.

result <- split(D_MtC, sub('\\d+', '', D_MtC$MS))

This will create a list where each element is a dataframe that corresponds to a unique value in the $MS column. The values are matched based on the pattern specified by the regular expression \\d+, which matches one or more digits.

Note: To print the result, use the following code:

for (i in 1:length(result)) {
  cat("Dataframe", i, ":\n")
  print(result[[i]])
}

This will print each dataframe in the list with its corresponding index.


Last modified on 2024-06-27