Skip to content

ENH: specify header=['list', 'of', 'headers'] in DataFrame.write_csv() #921

@brentp

Description

@brentp

As with R's write.table col.names which can take a list of column headers to output instead of the
headers.

here's a start, though I'm unsure about hwo it interacts with the index label stuff.

diff --git a/pandas/core/frame.py b/pandas/core/frame.py
index 248a4e9..8c93488 100644
--- a/pandas/core/frame.py
+++ b/pandas/core/frame.py
@@ -13,7 +13,7 @@ labeling information

 # pylint: disable=E1101,E1103
 # pylint: disable=W0212,W0231,W0703,W0622
-
+import collections
 from itertools import izip
 from StringIO import StringIO
 import csv
@@ -889,7 +889,9 @@ class DataFrame(NDFrame):
             cols = self.columns

         series = self._series
-        if header:
+        if isinstance(header, collections.Iterable):
+            writer.writerow([h or '' for h in header])
+        elif header:
             if index:
                 # should write something for index label
                 if index_label is None:

Metadata

Metadata

Assignees

No one assigned

    Labels

    EnhancementIO DataIO issues that don't fit into a more specific label

    Type

    No type

    Projects

    No projects

    Milestone

    Relationships

    None yet

    Development

    No branches or pull requests

    Issue actions