简体   繁体   English

Function 获取火花列表 dataframe 并转换为 pandas 然后 Z628CB5675FF524F3EZ19BAA7FAA

[英]Function to take a list of spark dataframe and convert to pandas then csv

import pyspark
            
dfs=[df1,df2,df3,df4,df5,df6,df7,df8,df9,df10,df1,df12,df13,df14,df15]
    
for x in dfs:
    y=x.toPandas()
    y.to_csv("D:/data")

This is what I wrote, but I actually want the function to take this list and convert every df into a pandas df and then convert it to csv and save it in the order as it appears on dfs list and save it to a particular directory in the order of name.这是我写的,但我实际上希望 function 获取此列表并将每个 df 转换为 pandas df 然后将其转换为 csv 列表中的特定目录并将其保存到特定目录中名字的顺序。 Is there a possible way to write such function?有没有办法写出这样的 function? PS D:/data is just an imaginary path and is used for explanation. PS D:/data 只是一个虚构的路径,用于解释。

If you will convert a dataframe to a csv, you still need to state it in df.to_csv .如果您要将dataframe转换为 csv,您仍然需要在 df.to_csv 中将其转换为df.to_csv So, try:所以,试试:

for x in dfs:
       y=x.toPandas()
       y.to_csv(f"D:/data/df{dfs.index(x) + 1}.csv")

I set it as df{dfs.index(x) + 1} so that the file names will be df1, df2, ... etc.我将其设置为df{dfs.index(x) + 1} ,这样文件名将是df1, df2, ...等。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM