Python Forum
Write from dictionary to excel - Printable Version

+- Python Forum (https://python-forum.io)
+-- Forum: Python Coding (https://python-forum.io/forum-7.html)
+--- Forum: Data Science (https://python-forum.io/forum-44.html)
+--- Thread: Write from dictionary to excel (/thread-38303.html)



Write from dictionary to excel - divon - Sep-27-2022

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.


RE: Write from dictionary to excel - Am1n3 - Oct-10-2022

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')



RE: Write from dictionary to excel - divon - Jun-10-2023

(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


RE: Write from dictionary to excel - Larz60+ - Jun-11-2023

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/