From d2bd6c3a08edf2d7b6490932abbf77697779ee0f Mon Sep 17 00:00:00 2001 From: eric Date: Sun, 18 Feb 2018 01:50:44 -0500 Subject: [PATCH 1/3] Add example of how to preserve order of columns with usecols. --- doc/source/io.rst | 6 +++++- pandas/io/parsers.py | 5 ++++- 2 files changed, 9 insertions(+), 2 deletions(-) diff --git a/doc/source/io.rst b/doc/source/io.rst index 7bb34e4d232dd..a0157a8156609 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -136,7 +136,11 @@ usecols : array-like or callable, default ``None`` that correspond to column names provided either by the user in `names` or inferred from the document header row(s). For example, a valid array-like `usecols` parameter would be ``[0, 1, 2]`` or ``['foo', 'bar', 'baz']``. - Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. + Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. Element + order is ignored, so usecols=[1,0] is the same as [0,1]. To instantiate a + DataFrame with element order preserved use ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` + for columns in ``[1, 0]`` order or ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` + for ``[0, 1]`` order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index 7ea6d321e0fdd..f5278293ee9d3 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -102,7 +102,10 @@ that correspond to column names provided either by the user in `names` or inferred from the document header row(s). For example, a valid array-like `usecols` parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element - order is ignored, so usecols=[1,0] is the same as [0,1]. + order is ignored, so usecols=[1,0] is the same as [0,1]. To instantiate a + DataFrame with element order preserved use + ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` for columns in ``[1, 0]`` + order or ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` for ``[0, 1]`` order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. An From f137bd0508ddf47c77f632c7d1be0785595388d5 Mon Sep 17 00:00:00 2001 From: eric Date: Sun, 18 Feb 2018 10:46:02 -0500 Subject: [PATCH 2/3] Encase usecols in double back ticks for consistency. Change column names from numeric to string. --- doc/source/io.rst | 11 ++++++----- pandas/io/parsers.py | 10 ++++++---- 2 files changed, 12 insertions(+), 9 deletions(-) diff --git a/doc/source/io.rst b/doc/source/io.rst index a0157a8156609..624633364f204 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -136,11 +136,12 @@ usecols : array-like or callable, default ``None`` that correspond to column names provided either by the user in `names` or inferred from the document header row(s). For example, a valid array-like `usecols` parameter would be ``[0, 1, 2]`` or ``['foo', 'bar', 'baz']``. - Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. Element - order is ignored, so usecols=[1,0] is the same as [0,1]. To instantiate a - DataFrame with element order preserved use ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` - for columns in ``[1, 0]`` order or ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` - for ``[0, 1]`` order. + Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. To + instantiate a DataFrame from ``data`` with element order preserved use + ``pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']]`` for columns + in ``['foo', 'bar']`` order or + ``pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']]`` for + ``['bar', 'foo']`` order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True: diff --git a/pandas/io/parsers.py b/pandas/io/parsers.py index f5278293ee9d3..4b1385514a0c4 100755 --- a/pandas/io/parsers.py +++ b/pandas/io/parsers.py @@ -102,10 +102,12 @@ that correspond to column names provided either by the user in `names` or inferred from the document header row(s). For example, a valid array-like `usecols` parameter would be [0, 1, 2] or ['foo', 'bar', 'baz']. Element - order is ignored, so usecols=[1,0] is the same as [0,1]. To instantiate a - DataFrame with element order preserved use - ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` for columns in ``[1, 0]`` - order or ``pd.read_csv(usecols=[0, 1])[[0, 1]]`` for ``[0, 1]`` order. + order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. + To instantiate a DataFrame from ``data`` with element order preserved use + ``pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']]`` for columns + in ``['foo', 'bar']`` order or + ``pd.read_csv(data, usecols=['foo', 'bar'])[['bar', 'foo']]`` + for ``['bar', 'foo']`` order. If callable, the callable function will be evaluated against the column names, returning names where the callable function evaluates to True. An From 68efc4bd3acb20acacd65f887385591db62b8394 Mon Sep 17 00:00:00 2001 From: eric Date: Sun, 18 Feb 2018 13:27:15 -0500 Subject: [PATCH 3/3] Add line to separate examples. --- doc/source/io.rst | 3 ++- 1 file changed, 2 insertions(+), 1 deletion(-) diff --git a/doc/source/io.rst b/doc/source/io.rst index 624633364f204..b7f8792b42fd6 100644 --- a/doc/source/io.rst +++ b/doc/source/io.rst @@ -135,7 +135,8 @@ usecols : array-like or callable, default ``None`` be positional (i.e. integer indices into the document columns) or strings that correspond to column names provided either by the user in `names` or inferred from the document header row(s). For example, a valid array-like - `usecols` parameter would be ``[0, 1, 2]`` or ``['foo', 'bar', 'baz']``. + `usecols` parameter would be ``[0, 1, 2]`` or ``['foo', 'bar', 'baz']``. + Element order is ignored, so ``usecols=[0, 1]`` is the same as ``[1, 0]``. To instantiate a DataFrame from ``data`` with element order preserved use ``pd.read_csv(data, usecols=['foo', 'bar'])[['foo', 'bar']]`` for columns