Skip to content

ENH: pandas read_* wildcard #15904 #16166

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 4 commits into from
Apr 30, 2017
Merged
Show file tree
Hide file tree
Changes from 2 commits
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
40 changes: 37 additions & 3 deletions doc/source/cookbook.rst
Original file line number Diff line number Diff line change
Expand Up @@ -910,9 +910,6 @@ The :ref:`CSV <io.read_csv_table>` docs
`appending to a csv
<http://stackoverflow.com/questions/17134942/pandas-dataframe-output-end-of-csv>`__

`how to read in multiple files, appending to create a single dataframe
<http://stackoverflow.com/questions/25210819/speeding-up-data-import-function-pandas-and-appending-to-dataframe/25210900#25210900>`__

`Reading a csv chunk-by-chunk
<http://stackoverflow.com/questions/11622652/large-persistent-dataframe-in-pandas/12193309#12193309>`__

Expand Down Expand Up @@ -943,6 +940,43 @@ using that handle to read.
`Write a multi-row index CSV without writing duplicates
<http://stackoverflow.com/questions/17349574/pandas-write-multiindex-rows-with-to-csv>`__

.. _cookbook.csv.multiple_files:

Reading multiple files to create a single DataFrame
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

The best way to combine multiple files into a single DataFrame is to read the individual frames one by one, put all
of the individual frames into a list, and then combine the frames in the list using :func:`pd.concat`:

.. ipython:: python

for i in range(3):
data = pd.DataFrame(np.random.randn(10, 4))
data.to_csv('file_{}.csv'.format(i))

frames = []
Copy link
Contributor

Choose a reason for hiding this comment

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

You can delete this frames now.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed, thanks.

files = ['file_0.csv', 'file_1.csv', 'file_2.csv']
result = pd.concat([pd.read_csv(f) for f in files], ignore_index=True)

You can use the same approach to read all files matching a pattern. Here is an example using ``glob``:

.. ipython:: python

import glob
frames = []
Copy link
Contributor

Choose a reason for hiding this comment

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

Same with this frames

Copy link
Contributor Author

Choose a reason for hiding this comment

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

Removed

files = glob.glob('file_*.csv')
result = pd.concat([pd.read_csv(f) for f in files], ignore_index=True)

Finally, this strategy will work with the other ``read_*(...)`` functions described in the :ref:`io docs<io>`.

.. ipython:: python
:supress:
for i in range(3):
os.remove('file_{}.csv'.format(i))

Parsing date components in multi-columns
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^

Parsing date components in multi-columns is faster with a format

.. code-block:: python
Expand Down
8 changes: 8 additions & 0 deletions doc/source/io.rst
Original file line number Diff line number Diff line change
Expand Up @@ -1439,6 +1439,14 @@ class of the csv module. For this, you have to specify ``sep=None``.
print(open('tmp2.sv').read())
pd.read_csv('tmp2.sv', sep=None, engine='python')

.. _io.multiple_files:

Reading multiple files to create a single DataFrame
'''''''''''''''''''''''''''''''''''''''''''''''''''

It's best to use :func:`~pandas.concat` to combine multiple files.
See the :ref:`cookbook<cookbook.csv.multiple_files>` for an example.

.. _io.chunking:

Iterating through files chunk by chunk
Expand Down