首页 > 解决方案 > Pandas Dataframe returns None after recursive function?

问题描述

I've written a simple script to save out the names of various subfolders into a spreadsheet. It seems to be doing its job at every point up to the return statement. It returns None...

If I add a print statement before the return I can see a populated dataFrame.

I guess I'm missing something obvious, would appreciate some help! Thanks

import sys, os, glob
from glob import glob
import pandas as pd

def findSubFoldersMultiple(iter,data_container):
    if iter > 0:
        current_directory = sys.argv[iter]
        directory_reformatted = sys.argv[iter] + "/*/"

        folders = glob(directory_reformatted )
        folders_stripped = [ folder.replace(sys.argv[iter],'').replace('/','') for folder in folders]

        curr_data_container = pd.DataFrame({ current_directory: folders_stripped })
        combined_data_container = pd.concat([data_container,curr_data_container],axis=1)
        findSubFoldersMultiple(iter-1,combined_data_container)
    else:
        print('Populated container in loop: \n' )
        print(data_container)
        return data_container

if len(sys.argv)<2:
    print ("Please specify directory/directories.")
else:
    writer = pd.ExcelWriter('subfolders.xlsx')
    empty_frame = pd.DataFrame({})
    populated_DF = findSubFoldersMultiple(len(sys.argv) - 1, empty_frame)
    print('Returned container: \n' )
    print(populated_DF)

标签: pythonpandas

解决方案


通过将 if 块中的最后一行更改为:

return findSubFoldersMultiple(iter-1,combined_data_container)

否则,您将返回基本情况(else 块)上的值,但不会在非基本情况递归调用链的上游返回它。


推荐阅读