Skip to content

[DOCS] Add example of how to preserve order of columns with usecols. #19746

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

Merged
merged 3 commits into from
Feb 21, 2018
Merged
Show file tree
Hide file tree
Changes from 1 commit
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 5 additions & 1 deletion doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -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]``.
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

can you add a blank line before the additions.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Sure thing. Please refer to 68efc4b

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
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Could you put these usecols=[] in double back ticks as well? And commas between the elements.

Finally, I have a slight preference for using names like ['foo', 'bar'] instead of [0, 1], even though it's a bit longer.

Copy link
Contributor Author

@EricChea EricChea Feb 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Done. Please refer to f137bd0

I went ahead and encased usecols in double back ticks.

Updated column names per your suggestion. I was originally trying to keep consistent with the line before Element order is ignored, so usecols=[0, 1] is the same as [1, 0].

DataFrame with element order preserved use ``pd.read_csv(usecols=[0, 1])[[0, 1]]``
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it should be “pd.read_csv(usecols=[0, 1])[[1, 0]]”

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Yep.

Copy link
Contributor Author

@EricChea EricChea Feb 18, 2018

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thanks for catching that. Fixed in commit f137bd0

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:
Expand Down
5 changes: 4 additions & 1 deletion pandas/io/parsers.py
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down