Skip to content
Advertisement

How to create files from a groupby object, based on the length of the dataframe

I have a dataframe (df) that looks like this (highly simplified):

JavaScript

The 'VALUE' column contains a variable number of rows with identical values. I am trying to output a series of csv files that contain all of the rows that contain a 'VALUE' length == 2, ==3 etc. For example:

JavaScript

I can get the desired output of one length value at a time, e.g., using:

JavaScript

However, I have dozens of values to test. I would like to put this in a for loop on the order of:

JavaScript
  • I’ve tried various constructions to iterate the groupby object using the items in the list, and I haven’t been able to make anything work.
  • It might be an issue with trying to use a groupby object this way, but it is more than likely it is my inability to get the syntax right to complete the iteration.

Advertisement

Answer

  • It doesn’t make sense to use a predetermined list to create the filenames.
  • df_len will be used to generate a filename using an f-string.
  • Path.exists() is used to determine if the file exists or not
JavaScript

  • If you must only create a file for dataframes of a specific length
JavaScript
Advertisement