我可以对大型数据集进行分组,并使用熊猫数据框制作多个CSV、excel文件。但是如何使用Pyspark数据框将700K记录分组到大约230组中,并在全国范围内生成230CSV文件
使用熊猫
grouped = df.groupby("country_code")
# run this to generate separate Excel files
for country_code, group in grouped:
group.to_excel(excel_writer=f"{country_code}.xlsx", sheet_name=country_code, index=False)
使用Pyspark数据帧,当我尝试喜欢它时-
for country_code, df_country in df.groupBy('country_code'):
print(country_code,df_country.show(1))
它回来了
TypeError: 'GroupedData' object is not iterable
如果您的要求是将所有国家/地区的数据保存在不同的文件中,您可以通过对数据进行分区来实现,但您将获得每个国家/地区的文件夹,而不是文件,因为spark无法将数据直接保存到文件中
Spark在调用数据帧编写器时创建文件夹
输出将是包含相应国家/地区数据的多个文件夹
如果您希望每个文件夹中都有一个文件,您可以将数据重新分区为
在编写时使用
partitionBy
,这样每个分区都基于您指定的列(country_code
)这是more关于这个
相关问题 更多 >
编程相关推荐