简体   繁体   中英

How do I index os.walk into a dataframe?

I'm trying to map my directory in a pandas dataframe but the auto index is always 0. Eventually I want to create a column that will MD5 the file paths. I know there are alternatives but I'm trying to do it in pandas. Here is my code:

for path, subFolders, files in os.walk(targetdir):
        for file in files:
            nm, ext = os.path.splitext(file)
            if ext.lower().endswith(('.xlsx','.m4v','mov')): #this is so i filter only what I want
                filepaths = os.path.join(os.path.abspath(path),file)


                df1 = pd.DataFrame({'filename': pd.Series(file), 
                                    'fullpath': pd.Series(filepaths,)
                                    })
                print df1

Thanks!

Collect the data in a list, and then create one DataFrame after the loops complete:

data = list()
for root, dirs, files in os.walk(targetdir):
    for filename in files:
        nm, ext = os.path.splitext(filename)
        if ext.lower().endswith(('.xlsx', '.m4v', 'mov')):
            fullpath = os.path.join(os.path.abspath(root), filename)
            data.append((filename, fullpath))
df1 = pd.DataFrame(data, columns=['filename', 'fullpath'])
print(df1)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM