函数未返回预期的数据框输出。

huangapple go评论95阅读模式
英文:

Function does not return expected dataframe output

问题

I'm a newbie in programming, python, nlp, and stackoverflow so grateful for your patience!

I have developed a function that extracts some text from several pdf files and sets up a pandas dataframe with it as well as other details from the original pdf files.

Outside of the function set up, these steps work well but once I 'package' them in the function I can't get the output to work and the resulting dataframe stays empty. I'm clearly missing something, help please! 函数未返回预期的数据框输出。

Here's the function (accessing text from relevant numbered file - Identified by Trustcode and Year).

  1. def Accessingtxt_func(Trustcode):
  2. DFTrust_Text1=pd.DataFrame(columns=['Text','Month','Year','Type', 'Trustcode'])
  3. for year in range(2021,2023):
  4. with open(os.path.join(mypath,f'{ts}Trust{Trustcode}-{year}a.txt'), 'w', encoding='utf-8') as fw:
  5. txt_content = extract_text(f'Trust{Trustcode}-{year}a.pdf')
  6. fw.write(txt_content)
  7. txt_content= txt_content.split('\n\n')
  8. DFTrust_Text1=DFTrust_Text1.append({'Text': txt_content, 'Year': {year}, 'Month':9, 'Type':1, 'Trustcode':Trustcode},ignore_index=True)
  9. return DFTrust_Text1
  10. year=year+1
  11. return DFTrust_Text1

The function compiles fine, and I then run it in a loop like this

  1. for Trustcode in range(12,14):
  2. print(Trustcode)
  3. Accessingtxt_func(Trustcode)
  4. DFTrust_Text1.head()

Which also runs fine, however I can't get it to provide the dataframe head and when calling the function in each loop step. Don't know why either.

I then still call the dataframe out after the loop like so

  1. DFTrust_Text1.head()

But I get an empty dataframe shell, not the expected dataframe with rows for Trustcodes 12,13 and years 2021 and 2022.

  1. Text Month Year Type Trustcode

I've tried with various positionings of the dataframe inside outside, global/local variable, but can't get it to work. Thanks for your help

英文:

I'm a newbie in programming, python, nlp and stackoverflow so grateful for your patience!

I have developed a function that extracts some text from several pdf files and sets up a pandas dataframe with it as well as other details from the original pdf files.

Outside of the function set up, these steps work well but once I 'package' them in the function I can't get the output to work and the resulting dataframe stays empty. I'm clearly missing something, help please! 函数未返回预期的数据框输出。

Here's the function (accessing text from relevant numbered file -Identified by Trustcode and Year).

  1. def Accessingtxt_func(Trustcode):
  2. DFTrust_Text1=pd.DataFrame(columns=['Text','Month','Year','Type', 'Trustcode'])
  3. for year in range(2021,2023):
  4. with open(os.path.join(mypath,f'{ts}Trust{Trustcode}-{year}a.txt'), 'w', encoding='utf-8') as fw:
  5. txt_content = extract_text(f'Trust{Trustcode}-{year}a.pdf')
  6. fw.write(txt_content)
  7. txt_content= txt_content.split('\n\n')
  8. DFTrust_Text1=DFTrust_Text1.append({'Text': txt_content, 'Year': {year}, 'Month':9, 'Type':1, 'Trustcode':Trustcode},ignore_index=True)
  9. return DFTrust_Text1
  10. year=year+1
  11. return DFTrust_Text1

The function compiles fine, and I then run it in a loop like this

  1. for Trustcode in range(12,14):
  2. print(Trustcode)
  3. Accessingtxt_func(Trustcode)
  4. DFTrust_Text1.head()

Which also runs fine, however I can't get it to provide the dataframe head and when calling the function in each loop step. Don't know why either.

I then still call the dataframe out after the loop like so

  1. DFTrust_Text1.head()

But I get an empty dataframe shell, not the expected dataframe with rows for Trustcodes 12,13 and years 2021 and 2022.

  1. Text Month Year Type Trustcode

I've tried with various positionings of the dataframe inside outside, global/local variable, but can't get it to work. Thanks for your help

答案1

得分: 0

需要在调用函数时分配一个数据框架:

  1. newdf = Accessingtxt_func(Trustcode)
  2. newdf.head()
英文:

You need to asign a dataframe when calling the function:

  1. newdf = Accessingtxt_func(Trustcode)
  2. newdf.head()

huangapple
  • 本文由 发表于 2023年7月24日 15:25:16
  • 转载请务必保留本文链接:https://go.coder-hub.com/76752222.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定