I am trying to list all files in folders into excel worksheet. The code I wrote can generate the result in the image attached (top one, but rows are not indexed), but I would like to have it formatted to the image at the bottom. How can I do that with python? Thanks!
import os
import pandas as pd
alist=[]
blist=[]
for folderName,subfolders,filenames in os.walk(r'C:\Users\2020'):
alist.append(filenames)
blist.append(str(folderName))
print(alist)
print(blist)
res_dct = {blist[i]: alist[i] for i in range(0, len(blist))}
df=pd.DataFrame.from_dict(res_dct,orient='index')
Given the DataFrame:
df = pd.DataFrame({
0: ['File1', 'Filex'],
1: ['File2', 'Filey'],
2: [None, 'Filez']
}, index=[r'C:\users', r'C:\users\paper'])
That looks like this:
>>> df
0 1 2
C:\users File1 File2 None
C:\users\paper Filex Filey Filez
You can create a MultiIndex based on your index/column combinations, then create a new DataFrame using that MultiIndex and the original DataFrame's data, properly reshaped:
index = pd.MultiIndex.from_product([df.index, df.columns])
new_df = pd.DataFrame(df.values.reshape(index.shape), index=index, columns=['filename'])
new_df
is:
>>> new_df
filename
C:\users 0 File1
1 File2
2 None
C:\users\paper 0 Filex
1 Filey
2 Filez
Use new_df.dropna()
if you don't care about the None
value in the filename row.
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.