[英]return multiple pandas.DataFrames from functions and export as different files?
def myfunc(arg1,arg2):
df1 = pd.Datframe() #uses arg1 and arg2; df1 has 10cols & 100rows
df2 = pd.Datframe() #uses arg1 and arg2; df2 has 4cols & 10rows
return df1, df2
result = myfunc(arg1,arg2)
I need to extract the individual df from result and export as .xlsx 我需要从结果中提取单个df并导出为.xlsx
Your function will return a tuple, which is iterable. 您的函数将返回一个可重复的元组。 You can export the results like this: 您可以这样导出结果:
import uuid
writer = pd.ExcelWriter('output.xlsx')
for r in result:
r.to_excel(writer, str(uuid.uuid4()))
This will write your files in .xlsx format, and assign each file a unique name. 这将以.xlsx格式写入文件,并为每个文件分配一个唯一的名称。
Hope this helps! 希望这可以帮助!
You can use sequence unpacking: 您可以使用序列解压缩:
def myfunc(arg1,arg2):
df1 = pd.Datframe() #uses arg1 and arg2; df1 has 10cols & 100rows
df2 = pd.Datframe() #uses arg1 and arg2; df2 has 4cols & 10rows
return df1, df2
df1, df2 = myfunc(arg1,arg2)
df1.to_excel('data1.xlsx')
df2.to_excel('data2.xlsx')
Or you can iterate in a loop: 或者您可以循环访问:
for idx, df in enumerate(myfunc(arg1, arg2)):
df.to_excel('data{0}.xlsx'.format(idx))
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.