使用 Dask pivot_table 后,我丢失了索引列
After using Dask pivot_table I lose the index column
在我对 Dask Dataframe 使用 pivot_table 并将数据保存到 Parquet 文件后,我丢失了索引列。
import dask.dataframe as dd
import pandas as pd
df=pd.DataFrame()
df["Index"]=[1,2,3,1,2,3]
df["Field"]=["A","A","A","B","B","B"]
df["Value"]=[10,20,30,100,120,130]
df
我的数据框:
Index Field Value
0 1 A 10
1 2 A 20
2 3 A 30
3 1 B 100
4 2 B 120
5 3 B 130
任务代码:
ddf=dd.from_pandas(df,2)
ddf=ddf.categorize("Field")
ddf=ddf.pivot_table(values="Value", index="Index", columns="Field")
dd.to_parquet("1.parq",ddf)
dd.read_parquet("1.parq").compute()
这给出了一个错误:
ValueError: Multiple possible indexes exist: ['A', 'B']. Please
select one with index='index-name'
我可以 select A 或 B 作为索引,但我缺少索引列。
我试过 dd.to_parquet("1.parq",ddf, write_index=True)
,但出现以下错误:
TypeError: cannot insert an item into a CategoricalIndex that is not
already an existing category
谁能帮我把列为 "Index" 的 table 保存到 Parquet 文件中?
PS:
ddf.pivot_table(values="Value", index="Index", columns="Field").compute()
给出了预期的结果:
Field A B
Index
1 10.0 100.0
2 20.0 120.0
3 30.0 130.0
并且使用 Pandas 不是解决方案,因为我的数据是 20 GB。
编辑:
我试过了
ddf.columns = list(ddf.columns)
dd.to_parquet("1.parq",ddf, write_index=True)
它给了我一个新的错误:
dask.async.TypeError: expected list of bytes
Google 表明此类错误是由 Tornado 异步库引起的。
这里有两个问题:
pivot_table
生成一个分类的列索引,因为您将原始列“Field”设为分类。将索引写入 parquet 在数据帧上调用 reset_index,并且 pandas 无法向列索引添加新值,因为它是分类的。您可以使用 ddf.columns = list(ddf.columns)
.
避免这种情况
索引列具有对象数据类型,但实际上包含整数。整数不是对象列中预期的类型之一,因此您应该转换它。
整个街区现在看起来像:
ddf = dd.from_pandas(df,2)
ddf = ddf.categorize("Field")
ddf = ddf.pivot_table(values="Value", index="Index", columns="Field")
ddf.columns = list(ddf.columns)
ddf = ddf.reset_index()
ddf['index'] = ddf.index.astype('int64')
dd.to_parquet("1.parq", ddf)
在我对 Dask Dataframe 使用 pivot_table 并将数据保存到 Parquet 文件后,我丢失了索引列。
import dask.dataframe as dd
import pandas as pd
df=pd.DataFrame()
df["Index"]=[1,2,3,1,2,3]
df["Field"]=["A","A","A","B","B","B"]
df["Value"]=[10,20,30,100,120,130]
df
我的数据框:
Index Field Value
0 1 A 10
1 2 A 20
2 3 A 30
3 1 B 100
4 2 B 120
5 3 B 130
任务代码:
ddf=dd.from_pandas(df,2)
ddf=ddf.categorize("Field")
ddf=ddf.pivot_table(values="Value", index="Index", columns="Field")
dd.to_parquet("1.parq",ddf)
dd.read_parquet("1.parq").compute()
这给出了一个错误:
ValueError: Multiple possible indexes exist: ['A', 'B']. Please select one with index='index-name'
我可以 select A 或 B 作为索引,但我缺少索引列。
我试过 dd.to_parquet("1.parq",ddf, write_index=True)
,但出现以下错误:
TypeError: cannot insert an item into a CategoricalIndex that is not already an existing category
谁能帮我把列为 "Index" 的 table 保存到 Parquet 文件中?
PS:
ddf.pivot_table(values="Value", index="Index", columns="Field").compute()
给出了预期的结果:
Field A B
Index
1 10.0 100.0
2 20.0 120.0
3 30.0 130.0
并且使用 Pandas 不是解决方案,因为我的数据是 20 GB。
编辑:
我试过了
ddf.columns = list(ddf.columns)
dd.to_parquet("1.parq",ddf, write_index=True)
它给了我一个新的错误:
dask.async.TypeError: expected list of bytes
Google 表明此类错误是由 Tornado 异步库引起的。
这里有两个问题:
避免这种情况pivot_table
生成一个分类的列索引,因为您将原始列“Field”设为分类。将索引写入 parquet 在数据帧上调用 reset_index,并且 pandas 无法向列索引添加新值,因为它是分类的。您可以使用ddf.columns = list(ddf.columns)
.索引列具有对象数据类型,但实际上包含整数。整数不是对象列中预期的类型之一,因此您应该转换它。
整个街区现在看起来像:
ddf = dd.from_pandas(df,2)
ddf = ddf.categorize("Field")
ddf = ddf.pivot_table(values="Value", index="Index", columns="Field")
ddf.columns = list(ddf.columns)
ddf = ddf.reset_index()
ddf['index'] = ddf.index.astype('int64')
dd.to_parquet("1.parq", ddf)