pyarrow 表添加列 '__index_level_0__

3 投票
2 回答
65 浏览
提问于 2025-04-14 17:45

如果我创建一个 pandas 数据框(df),然后把它转换成 pyarrow 表格,我会发现多了一个额外的列,叫做index_level_0

我该怎么去掉这个列呢?

from pyarrow import Table
import pandas as pd

df_empty = pd.DataFrame(columns=["a", "b", "c",])
df_empty = df_empty.astype(
    {"a": "int64", "b": "datetime64[ns]", "c": "int64"}
)
df = Table.from_pandas(df_empty)

在这里输入图片描述

2 个回答

1

我让它成功运行了,使用了 preserve_index=False 这个设置。

return Table.from_pandas(df_empty, preserve_index=False)
1

你可以试试把“preserve_index”设置为False。

from pyarrow import Table
import pandas as pd

df_empty = pd.DataFrame(columns=["a", "b", "c",])
df_empty = df_empty.astype(
  {"a": "int64", "b": "datetime64[ns]", "c": "int64"}
)
df = Table.from_pandas(df_empty, preserve_index = False)

撰写回答