How should I get the shape of a dask dataframe?

后端 未结 5 1570
醉话见心
醉话见心 2021-01-03 22:12

Performing .shape is giving me the following error.

AttributeError: \'DataFrame\' object has no attribute \'shape\'

How should

5条回答
  •  长情又很酷
    2021-01-03 22:18

    Well, I know this is a quite old question, but I had the same issue and I got an out-of-the-box solution which I just want to register here.

    Considering your data, I'm wondering that it is originally saved in a CSV similar file; so, for my situation, I just count the lines of that file (minus one, the header line). Inspired by this answer here, this is the solution I'm using:

    import dask.dataframe as dd
    from itertools import (takewhile,repeat)
     
    def rawincount(filename):
        f = open(filename, 'rb')
        bufgen = takewhile(lambda x: x, (f.raw.read(1024*1024) for _ in repeat(None)))
        return sum( buf.count(b'\n') for buf in bufgen )
    
    filename = 'myHugeDataframe.csv'
    df = dd.read_csv(filename)
    df_shape = (rawincount(filename) - 1, len(df.columns))
    print(f"Shape: {df_shape}")
    

    Hope this could help someone else as well.

提交回复
热议问题