Skip to content

ENH: add a comments variable to pandas.DataFrame.to_csv #59839

@ebo

Description

@ebo

Feature Type

  • Adding new functionality to pandas

  • Changing existing functionality in pandas

  • Removing existing functionality in pandas

Problem Description

For many years I have been adding comments to my Pandas generated CSV files by pre--generating the header, writing out the comments, and then appending the pandas to_csv output:

df = ....
with open('example.csv', mode='w') as f:
    f.write(','.join(df.keys())+'\n')
    f.write('# First comment\n')
    f.write('# Second comment...\n')

df.to_csv('example.csv', mode='a', index=False)

see StackOverflow post for examples: https://stackoverflow.com/questions/29233496/write-comments-in-csv-file-with-pandas

I think it would be easy to add a "comments" variable to to_csv to write out a list of comments in the header prepended with whatever the comment character is set:

my_comments = ["First comment",
               "Second comment..."]
df.to_csv('example.csv', comments=my_comments, index=False)

and it would produce a CSV similar to:

Var1, Var2
# First comment
# Second comment...
123, 456

I think this would be an easy add, and I am willing to code it up and make a pull request, but would like to see if there is any buy-in before spending the time.

Feature Description

It would be nice if pandas to_csv had a comments variable that worked something like:

my_comments = ["First comment",
               "Second comment..."]
df.to_csv('example.csv', comments=my_comments, index=False)

and it would produce a CSV similar to:

Var1, Var2
# First comment
# Second comment...
123, 456

Alternative Solutions

For years I have been adding comments to my Pandas generated CSV files by doing something like:

df = ....
with open('example.csv', mode='w') as f:
    f.write(','.join(df.keys())+'\n')
    f.write('# First comment\n')
    f.write('# Second comment...\n')

df.to_csv('example.csv', mode='a', index=False)

see StackOverflow post for examples: https://stackoverflow.com/questions/29233496/write-comments-in-csv-file-with-pandas

Additional Context

People have been using workarounds for a decade or more (see: https://stackoverflow.com/questions/29233496/write-comments-in-csv-file-with-pandas ), and it would be much more elegant if Pandas added the ability to inject comments into a CSV header.

Metadata

Metadata

Assignees

No one assigned

    Labels

    Closing CandidateMay be closeable, needs more eyeballsEnhancementIO CSVread_csv, to_csvNeeds DiscussionRequires discussion from core team before further action

    Type

    No type

    Projects

    No projects

    Milestone

    No milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions