执行 .shape 会出现以下错误。
AttributeError:“DataFrame”对象没有属性“shape”
我应该如何获得形状?
可以直接获取列数
len(df.columns) # this is fast
您还可以在数据帧本身上调用 len,但要注意这会触发计算。
len(df) # this requires a full scan of the data
Dask.dataframe 在不先通读所有数据的情况下不知道数据中有多少条记录。
有了形状,您可以执行以下操作
a = df.shape
a[0].compute(),a[1]
这将显示形状,就像熊猫显示的那样
嗯,我知道这是一个相当老的问题,但我遇到了同样的问题,并且我得到了一个开箱即用的解决方案,我只想在这里注册。
考虑到您的数据,我想知道它最初保存在类似 CSV 的文件中;因此,对于我的情况,我只计算该文件的行数(减去一,标题行)。受到这里的答案的启发,这是我正在使用的解决方案:
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}")
希望这也能帮助其他人。
print('(',len(df),',',len(df.columns),')')
要获得形状,我们可以尝试以下方法:
dask_dataframe.describe().compute()
索引的“count”列将给出行数
len(dask_dataframe.columns)
这将给出数据框中的列数
通过下面的代码获取列数。
import dask.dataframe as dd
dd1=dd.read_csv("filename.txt")
print(dd1.info)
#Output
<class 'dask.dataframe.core.DataFrame'>
Columns: 6 entries, CountryName to Value
dtypes: object(4), float64(1), int64(1)