@@ -946,7 +946,7 @@ Reading multiple files to create a single DataFrame
946
946
^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^^
947
947
948
948
The best way to combine multiple files into a single DataFrame is to read the individual frames one by one, put all
949
- of the individual frames into a list, and then combine the frames in the list using `` pd.concat ` `:
949
+ of the individual frames into a list, and then combine the frames in the list using :func: ` pd.concat `:
950
950
951
951
.. ipython :: python
952
952
@@ -956,22 +956,18 @@ of the individual frames into a list, and then combine the frames in the list us
956
956
957
957
frames = []
958
958
files = [' file_0.csv' , ' file_1.csv' , ' file_2.csv' ]
959
- for f in files:
960
- frames.append(pd.read_csv(f))
961
- result = pd.concat(frames)
959
+ result = pd.concat([pd.read_csv(f) for f in files], ignore_index = True )
962
960
963
961
You can use the same approach to read all files matching a pattern. Here is an example using ``glob ``:
964
962
965
963
.. ipython :: python
966
964
967
965
import glob
968
966
frames = []
969
- for f in glob.glob(' file_*.csv' ):
970
- frames.append(pd.read_csv(f))
971
- result = pd.concat(frames)
967
+ files = glob.glob(' file_*.csv' )
968
+ result = pd.concat([pd.read_csv(f) for f in files], ignore_index = True )
972
969
973
- This performs significantly better than using ``pd.append `` to add each of the files to an existing DataFrame.
974
- Finally, this strategy will work with the other ``read_ `` functions described in the :ref: `io docs<io> `.
970
+ Finally, this strategy will work with the other ``read_*(...) `` functions described in the :ref: `io docs<io> `.
975
971
976
972
.. ipython :: python
977
973
:supress:
0 commit comments