1. 程式人生 > >pandas.read_csv——分塊讀取大檔案

pandas.read_csv——分塊讀取大檔案

訪問本站觀看效果更佳

  • read_csv中有個引數chunksize,通過指定一個chunksize分塊大小來讀取檔案,返回的是一個可迭代的物件TextFileReader,IO Tools 舉例如下:
In [138]: reader = pd.read_table('tmp.sv', sep='|', chunksize=4)

In [139]: reader
Out[139]: <pandas.io.parsers.TextFileReader at 0x120d2f290>

In [140]: for chunk in reader:
   .....:     print(chunk)
   .....: 
   Unnamed: 0         0         1         2         3
0           0  0.469112 -0.282863 -1.509059 -1.135632
1           1  1.212112 -0.173215  0.119209 -1.044236
2           2 -0.861849 -2.104569 -0.494929  1.071804
3           3  0.721555 -0.706771 -1.039575  0.271860
   Unnamed: 0         0         1         2         3
0           4 -0.424972  0.567020  0.276232 -1.087401
1           5 -0.673690  0.113648 -1.478427  0.524988
2           6  0.404705  0.577046 -1.715002 -1.039268
3           7 -0.370647 -1.157892 -1.344312  0.844885
   Unnamed: 0         0        1         2         3
0           8  1.075770 -0.10905  1.643563 -1.469388
1           9  0.357021 -0.67460 -1.776904 -0.968914
  • 指定iterator=True 也可以返回一個可迭代物件TextFileReader :
In [141]: reader = pd.read_table('tmp.sv', sep='|', iterator=True)

In [142]: reader.get_chunk(5)
Out[142]: 
   Unnamed: 0         0         1         2         3
0           0  0.469112 -0.282863 -1.509059 -1.135632
1           1  1.212112 -0.173215  0.119209 -1.044236
2           2 -0.861849 -2.104569 -0.494929  1.071804
3           3  0.721555 -0.706771 -1.039575  0.271860
4           4 -0.424972  0.567020  0.276232 -1.087401

  • 我需要開啟的資料集是個csv檔案,大小為3.7G,並且對於資料一無所知,所以首先開啟前5行觀察資料的型別,列標籤等等:
chunks = pd.read_csv('train.csv',iterator = True)
chunk = chunks.get_chunk(5)
print chunk
'''
             date_time  site_name  posa_continent  user_location_country  \
0  2014-08-11 07:46:59          2               3                     66   
1  2014-08-11 08:22:12          2               3                     66   
2  2014-08-11 08:24:33          2               3                     66   
3  2014-08-09 18:05:16          2               3                     66   
4  2014-08-09 18:08:18          2               3                     66   

   user_location_region  user_location_city  orig_destination_distance  \
0                   348               48862                  2234.2641   
1                   348               48862                  2234.2641   
2                   348               48862                  2234.2641   
3                   442               35390                   913.1932   
4                   442               35390                   913.6259   

   user_id  is_mobile  is_package      ...        srch_children_cnt  \
0       12          0           1      ...                        0   
1       12          0           1      ...                        0   
2       12          0           0      ...                        0   
3       93          0           0      ...                        0   
4       93          0           0      ...                        0   

  srch_rm_cnt srch_destination_id  srch_destination_type_id  is_booking  cnt  \
0           1                8250                         1           0    3   
1           1                8250                         1           1    1   
2           1                8250                         1           0    1   
3           1               14984                         1           0    1   
4           1               14984                         1           0    1   

   hotel_continent  hotel_country  hotel_market  hotel_cluster  
0                2             50           628              1  
1                2             50           628              1  
2                2             50           628              1  
3                2             50          1457             80  
4                2             50          1457             21  

[5 rows x 24 columns]
'''