0

I have this code that is working but i want to modify it such that i acheive the result with out writing and reading to excel file. Intended out put is given in the end

 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}

Intended Output

dfs['red']

enter image description here

gmm005
  • 27
  • 5

1 Answers1

0

The pandas have the method .to_dict to do such a things. you can do mydict = pd.to_dict() and save it to a variable. see this

bmalbusca
  • 353
  • 1
  • 4
  • 15