Python Forum
Write from dictionary to excel
Thread Rating:
  • 0 Vote(s) - 0 Average
  • 1
  • 2
  • 3
  • 4
  • 5
Write from dictionary to excel
#1
Hello everyone, I would like to ask about how to write from a dictionary into excel.

The program that I have written so far:

import numpy as np
import pandas as pd

n = 3
N = pow(2, n)

ilr_arr = np.zeros((n+1, N))
ilr_dict = dict()
for idp in range(n + 1):
    a = pow(2, idp)
    b = pow(2, n - idp)
    ilr_dict[idp] = np.reshape(ilr_arr[idp], (a, b))
    for ing in range(a):
        interleaver = np.arange(pow(2, n - idp))
        np.random.shuffle(interleaver)
        ilr_dict[idp][ing] = interleaver
    # df = pd.DataFrame(data=ilr_dict[idp], index=[0]) when I use this syntax, it become error.
If there are any suggestions about this, please tell me.
Reply
#2
Hi divon,
The answer to your request by using df.to_excel() method, which creates the excel file then writes your dataframe into it,
import numpy as np
import pandas as pd

n = 3
N = pow(2, n)

ilr_arr = np.zeros((n+1, N))
ilr_dict = dict()
df = pd.DataFrame
for idp in range(n + 1):
    a = pow(2, idp)
    b = pow(2, n - idp)
    ilr_dict[idp] = np.reshape(ilr_arr[idp], (a, b))
    for ing in range(a):
        interleaver = np.arange(pow(2, n - idp))
        np.random.shuffle(interleaver)
        ilr_dict[idp][ing] = interleaver
        df=pd.DataFrame(data=ilr_dict[idp])
print(df)
df.to_excel('saveMyOutput.xlsx')
Gribouillis likes this post
Reply
#3
(Oct-10-2022, 10:18 PM)Am1n3 Wrote: Hi divon,
The answer to your request by using df.to_excel() method, which creates the excel file then writes your dataframe into it,
import numpy as np
import pandas as pd

n = 3
N = pow(2, n)

ilr_arr = np.zeros((n+1, N))
ilr_dict = dict()
df = pd.DataFrame
for idp in range(n + 1):
    a = pow(2, idp)
    b = pow(2, n - idp)
    ilr_dict[idp] = np.reshape(ilr_arr[idp], (a, b))
    for ing in range(a):
        interleaver = np.arange(pow(2, n - idp))
        np.random.shuffle(interleaver)
        ilr_dict[idp][ing] = interleaver
        df=pd.DataFrame(data=ilr_dict[idp])
print(df)
df.to_excel('saveMyOutput.xlsx')

Thank you very much for your reply, @Am1n3. It really helps me to proceed with my task. Big Grin
Reply
#4
There are many packages that deal with excel.
Not something that I do often, but when I do, I tend to use openpyxl
Now, there may be better choices.
PyPi: https://pypi.org/project/openpyxl/
ReadTheDocs: https://openpyxl.readthedocs.io/en/stable/
Reply


Possibly Related Threads…
Thread Author Replies Views Last Post
  Split excel file and write output at specific row and set sheet position DSCA 0 1,959 May-12-2022, 07:29 PM
Last Post: DSCA
  Write a dictionary with arrays as values into JSON format paul18fr 3 5,512 Oct-20-2021, 10:38 AM
Last Post: buran
  [Pandas] Write data to Excel with dot decimals manonB 1 5,774 May-05-2021, 05:28 PM
Last Post: ibreeden
  Read exel with merged cells and write to another excel SriMekala 0 2,938 Aug-10-2019, 07:14 AM
Last Post: SriMekala
  how read and write merged cells in excel SriMekala 1 15,061 Aug-07-2019, 11:27 PM
Last Post: scidam
  Python write result of VAR to excel file wissam1974 8 8,523 Jul-13-2019, 01:09 PM
Last Post: wissam1974
  Pandas - Write to Exisitng Excel File - Sorted List dj99 4 14,755 Jul-29-2018, 07:56 AM
Last Post: dj99
  How do I write a line of code into an excel spreadsheet using Python code? Emerogork 2 3,153 Feb-07-2018, 06:54 PM
Last Post: snippsat
  Write data into existing Excel (xlsx) file with multiple sheets BNB 1 15,306 Jun-01-2017, 04:22 PM
Last Post: Larz60+

Forum Jump:

User Panel Messages

Announcements
Announcement #1 8/1/2020
Announcement #2 8/2/2020
Announcement #3 8/6/2020