我正在尝试使用s3fs库和 pandas在 S3 上将数据帧编写为 CSV 文件。尽管有文档,但恐怕 gzip 压缩参数不适用于 s3fs。
def DfTos3Csv (df,file):
with fs.open(file,'wb') as f:
df.to_csv(f, compression='gzip', index=False)
Run Code Online (Sandbox Code Playgroud)
此代码将数据帧保存为 S3 中的新对象,但保存为纯 CSV 而非 gzip 格式。另一方面,使用此压缩参数可以正常工作的读取功能。
def s3CsvToDf(file):
with fs.open(file) as f:
df = pd.read_csv(f, compression='gzip')
return df
Run Code Online (Sandbox Code Playgroud)
写入问题的建议/替代方案?先感谢您!。
我有一个具有以下结构的Pyspark数据框:
root
|-- Id: string (nullable = true)
|-- Q: array (nullable = true)
| |-- element: struct (containsNull = true)
| | |-- pr: string (nullable = true)
| | |-- qt: double (nullable = true)
Run Code Online (Sandbox Code Playgroud)
类似于:
+----+--------------------- ... --+
| Id | Q |
+----+---------------------- ... -+
| 001| [ [pr1,1.9], [pr3,2.0]...] |
| 002| [ [pr2,1.0], [pr9,3.9]...] |
| 003| [ [pr2,9.0], ... ] |
...
Run Code Online (Sandbox Code Playgroud)
我想将Q数组转换为列(名称pr值qt)。我也想通过合并(添加)相同的列来避免重复的列。
+----+-----+-----+------+ ... ----+
| Id | pr1 | pr2 | …Run Code Online (Sandbox Code Playgroud)