【发布时间】:2020-11-07 05:21:33
【问题描述】:
我有一个列表,其中的输入文件存储为代表导入的 Excel 文件的字典。每个字典都有多个存储为 DataFrames 的工作表。
当三个 NaN 值出现在列中时,我想存储彼此附加的不同字典(Excel 文件)的相同键(工作表),同时打破工作表。我初始化了一个新字典,它应该具有与字典(Excel 文件)相同的键(表),但现在所有数据都附加到这个字典 d_sheets
为此,我使用以下代码:
input_files = []
for file in read_input:
input_file = pd.read_excel(io=file, sheet_name=needed_sheets, dtype=str)
input_files.append(input_file)
d_sheets = {}
for dictionary in input_files:
for sheet_name in sorted(dictionary):
d_sheets[sheet_name] = pd.DataFrame()
if sheet_name != 'Sheetname1':
cell = dictionary[sheet_name]['Columnname1']
else:
cell = dictionary[sheet_name]['Columnname2']
three_NaNs = cell.isna() & cell.shift(-1).isna() & cell.shift(-2).isna()
first_instance = cell[three_NaNs].index.min()
good_data = dictionary[sheet_name][cell.index <= first_instance]
d_sheets[sheet_name].append(good_data)
d_sheets[sheet_name] = pd.concat([d_sheets[sheet_name], good_data], axis=0)
看来,对于单个字典(Excel 文件),布尔语句完成了它们的工作,并且密钥存储在 d_sheets 中。但是,我找不到循环遍历 input_files 中的字典的工作方法。
什么可能导致我无法遍历列表input_files 的项目并将所有工作表存储在d_sheets 中的问题?
【问题讨论】:
标签: python excel pandas dictionary