我有这个工作的代码,但我想修改它,这样我就可以在不写入和读取 excel 文件的情况下获得结果。最后给出预期输出
df = pd.DataFrame({'a': ['red', 'yellow', 'red'], 'b': [0.5, 0.25, 0.125],'datetime':[2019-1-1, 2019-5-1, 2019-6-1],})
writer = pd.ExcelWriter('f.xlsx') ### need to modify this to get dfs a dictionary without wriitng to excel
for unique_var in df['a'].unique():
frame = df[df['a'] == unique_var]
frame.to_excel(writer, sheet_name=unique_var,index=False)
writer.save()
xl_file = pd.ExcelFile('f.xlsx')
dfs = {sheet_name: xl_file.parse(sheet_name) ### dfs is the dictionary of data frames
for sheet_name in xl_file.sheet_names}
预期输出
dfs['red']