使用Pyarrow将.parquet文件转换为CSV

4

我有一个.parquet文件,并且正在使用PyArrow。我使用以下代码将.parquet文件转换为表格:

import pyarrow.parquet as pq
import pandas as pd
filepath = "xxx"  # This contains the exact location of the file on the server
from pandas import Series, DataFrame
table = pq.read_table(filepath)

执行table.shape返回(39014行, 19列)

表的模式为:

col1: int64 not null
col2: string not null
col3: string not null
col4: int64 not null
col5: string not null
col6: string not null
col7: int64 not null
col8: int64 not null
col9: string not null
col10: string not null
col11: string not null
col12: string not null
col13: string not null
col14: string not null
col15: string not null
col16: int64 not null
col17: int64 not null
col18: int64 not null
col19: string not null

执行p = table.to_pandas()时,我遇到了以下错误:

ImportError: 无法导入名称RangeIndex

如何将此parquet文件转换为数据框架,然后再转换为CSV格式? 请帮忙。谢谢。

2
你使用的是哪个版本的pyarrow和pandas?它们可能不兼容。最近几天,Pandas发布了一个新版本,PyArrow也将发布一个新版本。现在可能有帮助的是升级/降级你的Pandas安装,直到新的PyArrow版本发布为止。 - Uwe L. Korn
尝试使用 from pandas import RangeIndex 并更新您的问题以显示输出。 - Sergey Bushmanov
1个回答

3
尝试以下方法:
import pyarrow as pa
import pyarrow.parquet as pq
import pandas as pd
import pyodbc

def read_pyarrow(path, nthreads=1):
    return pq.read_table(path, nthreads=nthreads).to_pandas()

path = './test.parquet'
df1 = read_pyarrow(path)

df1.to_csv(
    './test.csv',
    sep='|',
    index=False,
    mode='w',
    line_terminator='\n',
    encoding='utf-8')

从技术上讲,如果分隔符是“|”,那么它不是CSV,但原理是相同的 :-) - kellyfj

网页内容由stack overflow 提供, 点击上面的
可以查看英文原文,
原文链接