【发布时间】:2018-10-25 14:32:38
【问题描述】:
执行 .shape 给我以下错误。
AttributeError: 'DataFrame' 对象没有属性 'shape'
我应该如何获得形状?
【问题讨论】:
执行 .shape 给我以下错误。
AttributeError: 'DataFrame' 对象没有属性 'shape'
我应该如何获得形状?
【问题讨论】:
可以直接获取列数
len(df.columns) # this is fast
您也可以在数据帧本身上调用 len,但要注意这会触发计算。
len(df) # this requires a full scan of the data
Dask.dataframe 不知道您的数据中有多少条记录,而无需先阅读所有记录。
【讨论】:
df.index.size.compute(),它比运行len(df) 更快……但我的数据存储在柱状拼花中……所以这取决于你的底层数据架构是什么。
要得到形状,我们可以这样尝试:
dask_dataframe.describe().compute()
索引的“count”列会给出行数
len(dask_dataframe.columns)
这将给出数据框中的列数
【讨论】:
嗯,我知道这是一个很老的问题,但我遇到了同样的问题,我得到了一个开箱即用的解决方案,我只想在这里注册。
考虑到您的数据,我想知道它最初保存在 CSV 类似文件中;所以,就我的情况而言,我只计算该文件的行数(减一,标题行)。受this answer here 启发,这是我正在使用的解决方案:
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}")
希望这对其他人也有帮助。
【讨论】:
使用形状,您可以执行以下操作
a = df.shape
a[0].compute(),a[1]
这将购买与熊猫一样的形状
【讨论】:
print('(',len(df),',',len(df.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)
【讨论】: