Skip to content

Commit d2bd6c3

Browse files
author
eric
committed
Add example of how to preserve order of columns with usecols.
1 parent c8d8331 commit d2bd6c3

File tree

2 files changed

+9
-2
lines changed

2 files changed

+9
-2
lines changed

doc/source/io.rst

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,11 @@ usecols : array-like or callable, default ``None``
136136
that correspond to column names provided either by the user in `names` or
137137
inferred from the document header row(s). For example, a valid array-like
138138
`usecols` parameter would be ``[0, 1, 2]`` or ``['foo', 'bar', 'baz']``.
139-
Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``.
139+
Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. Element
140+
order is ignored, so usecols=[1,0] is the same as [0,1]. To instantiate a
141+
DataFrame with element order preserved use ``pd.read_csv(usecols=[0, 1])[[0, 1]]``
142+
for columns in ``[1, 0]`` order or ``pd.read_csv(usecols=[0, 1])[[0, 1]]``
143+
for ``[0, 1]`` order.
140144

141145
If callable, the callable function will be evaluated against the column names,
142146
returning names where the callable function evaluates to True:

pandas/io/parsers.py

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -102,7 +102,10 @@
102102
that correspond to column names provided either by the user in `names` or
103103
inferred from the document header row(s). For example, a valid array-like
104104
`usecols` parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element
105-
order is ignored, so usecols=[1,0] is the same as [0,1].
105+
order is ignored, so usecols=[1,0] is the same as [0,1]. To instantiate a
106+
DataFrame with element order preserved use
107+
``pd.read_csv(usecols=[0, 1])[[0, 1]]`` for columns in ``[1, 0]``
108+
order or ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` for ``[0, 1]`` order.
106109
107110
If callable, the callable function will be evaluated against the column
108111
names, returning names where the callable function evaluates to True. An

0 commit comments

Comments
 (0)