How do I read a large csv file with pandas?

后端 未结 15 1881
隐瞒了意图╮
隐瞒了意图╮ 2020-11-21 07:12

I am trying to read a large csv file (aprox. 6 GB) in pandas and i am getting a memory error:

MemoryError                               Traceback (most recen         


        
15条回答
  •  后悔当初
    2020-11-21 07:37

    The function read_csv and read_table is almost the same. But you must assign the delimiter “,” when you use the function read_table in your program.

    def get_from_action_data(fname, chunk_size=100000):
        reader = pd.read_csv(fname, header=0, iterator=True)
        chunks = []
        loop = True
        while loop:
            try:
                chunk = reader.get_chunk(chunk_size)[["user_id", "type"]]
                chunks.append(chunk)
            except StopIteration:
                loop = False
                print("Iteration is stopped")
    
        df_ac = pd.concat(chunks, ignore_index=True)
    

提交回复
热议问题