简体   繁体   English

如何使用新 function 中一个 function 的返回值(列表)在用户所需的文件路径中将列表打印为 csv

[英]How to Use the Returned Value (a list) from one function in a new function to print the list as a csv at the user's desired file path

 def find_qualifying_loans(bank_data, credit_score, debt, income, loan, home_value):
      monthly_debt_ratio = calculate_monthly_debt_ratio(debt, income)
      print(f"The monthly debt to income ratio is {monthly_debt_ratio:.02f}")

      # Calculate loan to value ratio
      loan_to_value_ratio = calculate_loan_to_value_ratio(loan, home_value)
      print(f"The loan to value ratio is {loan_to_value_ratio:.02f}.")

      # Run qualification filters
      bank_data_filtered = filter_max_loan_size(loan, bank_data)
      bank_data_filtered = filter_credit_score(credit_score, bank_data_filtered)
      bank_data_filtered = filter_debt_to_income(monthly_debt_ratio, bank_data_filtered)
      bank_data_filtered = filter_loan_to_value(loan_to_value_ratio, bank_data_filtered)

      print(f"Found {len(bank_data_filtered)} qualifying loans")

      return bank_data_filtered

def save_qualifying_loans(qualifying_loans)    

This new function is supposed to first verify if the amount of qualifying loans is 'None', in which the program can terminate after a goodbye message.这个新的 function 应该首先验证合格贷款的数量是否为“无”,其中程序可以在再见消息后终止。 Next, if there are qualifying loans, I need to save the qualifying loans from above found in the return value 'bank_data_filtered' as a csv file.接下来,如果有符合条件的贷款,我需要将上面在返回值“bank_data_filtered”中找到的符合条件的贷款保存为 csv 文件。 The program user is supposed to select the desired output path on their computer.程序用户应该在他们的计算机上 select 所需的 output 路径。 How do I even begin writing this new function?我什至如何开始编写这个新的 function? Thank you.谢谢你。

Something like this?像这样的东西?

import pandas as pd
import os
df = pd.DataFrame({'name': ['Raphael', 'Donatello'],
                   'mask': ['red', 'purple'],
                   'weapon': ['sai', 'bo staff']})


def saveCsv(df,filepath, filename):
    df.to_csv(os.path.join(filepath,filename))

saveCsv(df,filepath='/home/ashleyubuntu/Documents',filename='df.csv')

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

相关问题 如何在另一个 function 中使用返回的列表? - How to use a returned list from one function in another? 如何使用函数返回的列表,作为 Python 中的函数参数 - How to use returned list from function, as function args in Python 如何从字典中获取输入? 并打印所需的值..我想使用输入 function 并打印“zinger burer” - how to take input from dictionaries ? and print that desired value ..i want to use input function and print “zinger burer” 如何使用 map function 使用 python 将数据帧列表保存到所需路径 - How to use map function to save a list of dataframes to the desired path using python Python,使用从 function 返回的字典列表 - Python, use a list of dictionaries returned from a function 打印从函数返回的文件 - Print a returned file from a function 如何使用列表理解来读取 function 并创建一个新列表? - How to use a list comprehension to read from a function and create a new list? 如何在机器人文件中使用python函数的多个返回值? - How to use multiple returned value from a python function in a robot file? 如何在第二个 function 中使用来自一个 function 的列表中的值 - How to use values in list from one function in a second function map函数返回的列表在使用一次后消失 - List returned by map function disappears after one use
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM