How to read data in Python dataframe without concatenating?
<p>I want to read the file f (file size:85GB) in chunks to a dataframe. Following code is suggested.</p>

<pre><code>chunksize = 5
TextFileReader = pd.read_csv(f, chunksize=chunksize)
</code></pre>

<p>However, this code gives me TextFileReader, not dataframe. Also, I don't want to concatenate these chunks to convert TextFileReader to dataframe because of the memory limit. Please advise.</p>

<p>As you are trying to process 85GB CSV file, if you will try to read all the data by breaking it into chunks and converting it into dataframe then it will hit memory limit for sure. You can try to solve this problem by using different approach. In this case, you can use filtering operations on your data. For example, if there are 600 columns in your dataset and you are interested only in 50 columns. Try to read only 50 columns from the file. This way you will save lot of memory. Process your rows as you read them. If you need to filter the data first, use a generator function. <code>yield</code> makes a function a generator function, which means it won't do any work until you start looping over it.</p>

<p>For more information regarding generator function: 
<a href="http://stackoverflow.com/questions/17444679/reading-a-huge-csv-in-python">Reading a huge .csv in python</a></p>

<p>For efficient filtering refer: <a href="http://codereview.stackexchange.com/questions/88885/efficiently-filter-a-large-100gb-csv-file-v3">http://codereview.stackexchange.com/questions/88885/efficiently-filter-a-large-100gb-csv-file-v3</a>  </p>

<p>For processing smaller dataset:</p>

<p><strong>Approach 1: To convert reader object to dataframe directly:</strong></p>

<pre><code>full_data = pd.concat(TextFileReader, ignore_index=True)
</code></pre>

<p>It is necessary to add parameter <a href="http://pandas.pydata.org/pandas-docs/stable/merging.html#ignoring-indexes-on-the-concatenation-axis" rel="nofollow">ignore index</a> to function concat, because avoiding duplicity of indexes.</p>

<p><strong>Approach 2:</strong> <strong>Use Iterator or get_chunk to convert it into dataframe.</strong> </p>

<p>By specifying a chunksize to read_csv,return value will be an iterable object of type TextFileReader. </p>

<pre><code>df=TextFileReader.get_chunk(3)

for chunk in TextFileReader:
    print(chunk)
</code></pre>

<p>Source : <a href="http://pandas.pydata.org/pandas-docs/stable/io.html#io-chunking" rel="nofollow">http://pandas.pydata.org/pandas-docs/stable/io.html#io-chunking</a></p>

<p><code>df= pd.DataFrame(TextFileReader.get_chunk(1))</code> </p>

<p>This will convert one chunk to dataframe. </p>

<p><strong>Checking total number of chunks in TextFileReader</strong></p>

<pre><code>number_of_chunks=0

for chunk in TextFileReader:
   number_of_chunks=number_of_chunks+1 


print(number_of_chunks)
</code></pre>

<p>If file size is bigger,I won't recommend second approach. For example, if csv file consist of 100000 records then chunksize=5 will create 20,000 chunks.   </p>

python csv pandas dataframe chunks