【问题标题】:Iterating through files and passing each one through a function遍历文件并通过函数传递每个文件
【发布时间】:2020-02-10 09:28:07
【问题描述】:

我正在尝试构建一个预算计算器来练习 python。目前我正在尝试遍历目录中的文件,然后将每个文件传递给一个函数以将我需要的数据提取到 DataFrame(准备好对其进行计算)。

我已经设法创建了清理数据的函数,以及一个遍历文件的 for 循环。但是,我无法弄清楚如何为每次迭代附加 DataFrame。

#Where to look
os.chdir(r"C:\relevant\directory")
cwd = os.getcwd()

#key variables
main_df = pd.DataFrame()
pay_slip = {}
master_df = pd.DataFrame()

#Iterate over files
for file in os.listdir():
    slip_content = read_pdf(file)
    pay_slip[file] = slip_content

#Data clean up function
def get_key_info(pay_slip):
    read_dictionary = pay_slip.get(file)
    salary_str = read_dictionary["Employee"].iloc[2]
    pay_after_tax_str = read_dictionary["Tax Period"].iloc[14]
    date_format = read_dictionary["Pay Date"].iloc[0]
    salary = int(float(salary_str[1:].replace(",", "")))
    pay = int(float(pay_after_tax_str[1:].replace(",", "")))
    deductions = (salary - pay)
    df = pd.DataFrame([
        [date_format, salary, pay, deductions]
        ],
        columns=["Payment date", "Salary before tax", "take home pay", "total deductions"])
    return df

print(get_key_info(pay_slip))

当我运行此代码时,只有一个文件被添加到 DataFrame,而不是所有文件。

提前感谢您的帮助

【问题讨论】:

    标签: python pandas tabula


    【解决方案1】:

    您不会遍历您的 pay_slip 字典。

    
    for file in os.listdir(): 
        slip_content = read_pdf(file) 
        pay_slip[file] = slip_content 
    
    #Data clean up function
    def get_key_info(pay_slip): 
        read_dictionary = pay_slip.get(file) #<= where is file variable assign?
    
    

    【讨论】:

      【解决方案2】:

      感谢弗洛里安的帮助,我已经解决了这个循环问题,就像你说的那样。

      但是,我无法遍历字典,因为它不可散列。

      我将在下面发布我的代码,以防其他人遇到与我相同的问题。

          #Where to look
      os.chdir(r"C:\relevant\directory")
      cwd = os.getcwd()
      
      #key variables
      master_df = pd.DataFrame()
      
      
      #Data clean up function
      def get_key_info(x):
          salary_str = get_data["Employee"].iloc[2]
          pay_after_tax_str = get_data["Tax Period"].iloc[14]
          date_format = get_data["Pay Date"].iloc[0]
          salary = int(float(salary_str[1:].replace(",", "")))
          pay = int(float(pay_after_tax_str[1:].replace(",", "")))
          deductions = (salary - pay)
          df = pd.DataFrame([
              [date_format, salary, pay, deductions]
              ],
              columns=["Payment date", "Salary before tax", "take home pay", "total deductions"])
          return df
      
      #Iterate over files
      for f in os.listdir():
          get_data = read_pdf(f)
          master_df = master_df.append(get_key_info(f), ignore_index = True)
      
      print(master_df)
      

      这里我设置了变量get_data来改变for循环的每次迭代,然后.append()the master_df

      【讨论】:

        猜你喜欢
        • 2020-10-24
        • 1970-01-01
        • 2021-12-02
        • 1970-01-01
        • 2017-09-30
        • 1970-01-01
        • 2022-01-09
        • 2021-07-09
        • 1970-01-01
        相关资源
        最近更新 更多