我创建了一个试用文件夹,我认为它与您的相似。我只为 Person1 和 Person3 添加了数据。
在附图中,名为 Person1 和 Person3 的文件是导出的文件,仅包含每个人的第二列。所以现在每个人都有自己的文件了。
我在每一行的作用上添加了一个简短的描述。如果有什么不清楚的地方请告诉我。
import pandas as pd
import glob
path = r'C:\..\trial' # use your path where the files are
all_files = glob.glob(path + "/*.xlsx") # will get you all files with an extension .xlsx in a folder
li = []
for i in range(0,51): # numbers from 1 to 50 (for the 50 different people)
for f in all_files:
if str(i) in f: # checks if the number (i) is in the excel name
df = pd.read_excel(f,
sheet_name=0, # import 1st sheet
usecols=([1])) # only import column 2
df['person'] = f.rsplit('\\',1)[1].split('_')[0] # get the name of the person in a column
li.append(df) # add it to the list of dataframes
all_person = pd.concat(li, axis=0, ignore_index=True) # concat all dataframes imported
然后就可以导出到同一个路径,每个不同的人一个不同的excel文件
for i,j in all_person.groupby('person'):
j.to_excel(f'{path}\{i}.xlsx', index = False)
我知道这可能不是最有效的方法,但它可能会为您提供所需的东西。