Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Convenience functions for writing CSV #59

Open
abelsiqueira opened this issue Jun 28, 2024 · 0 comments
Open

Convenience functions for writing CSV #59

abelsiqueira opened this issue Jun 28, 2024 · 0 comments

Comments

@abelsiqueira
Copy link
Member

Description

Create a function that saves a table as CSV, possibly adding the prefixes row.

Taken from TulipaClustering:

"""
    write_csv_with_prefixes(file_path, df; prefixes)

Writes the dataframe `df` into a csv file at `file_path`. If `prefixes` are
provided, they are written above the column names. For example, these prefixes
can contain metadata describing the columns.
"""
function write_csv_with_prefixes(file_path, df; prefixes = nothing, csvargs...)
  if isnothing(prefixes) || length(prefixes) == 0
    # If there are no prefixes, just write the data frame into the file
    CSV.write(file_path, df; strict = true, csvargs...)
  else
    # Convert the prefixes to a one-row table for `CSV.write` to use
    prefixes = reshape(prefixes, (1, length(prefixes))) |> Tables.table
    # First we write the prefixes, then we append the data drom the dataframe
    CSV.write(file_path, prefixes; header = false, strict = true, csvargs...)
    CSV.write(file_path, df; header = true, append = true, strict = true, csvargs...)
  end
  return nothing
end

Motivation

No response

Target audience

No response

Can you help

No response

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
None yet
Projects
None yet
Development

No branches or pull requests

1 participant