python copy dictionary into excel
I have this dictionary:
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
how can I copy the dictionary into an excel file?
I would like this output:
Name Surname Age
Bob Red 17
thank you!
python
add a comment |
I have this dictionary:
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
how can I copy the dictionary into an excel file?
I would like this output:
Name Surname Age
Bob Red 17
thank you!
python
You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyworddictto name a variable.
– Mark
Nov 21 '18 at 18:45
add a comment |
I have this dictionary:
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
how can I copy the dictionary into an excel file?
I would like this output:
Name Surname Age
Bob Red 17
thank you!
python
I have this dictionary:
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
how can I copy the dictionary into an excel file?
I would like this output:
Name Surname Age
Bob Red 17
thank you!
python
python
edited Nov 21 '18 at 18:48
luana nastasi
asked Nov 21 '18 at 18:41
luana nastasiluana nastasi
113
113
You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyworddictto name a variable.
– Mark
Nov 21 '18 at 18:45
add a comment |
You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyworddictto name a variable.
– Mark
Nov 21 '18 at 18:45
You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword
dict to name a variable.– Mark
Nov 21 '18 at 18:45
You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword
dict to name a variable.– Mark
Nov 21 '18 at 18:45
add a comment |
3 Answers
3
active
oldest
votes
import pandas as pd
pd.DataFrame(dict1).to_excel('myfile.xslx')
add a comment |
If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:
import xlwt
from datetime import datetime
wb = xlwt.Workbook()
ws = wb.add_sheet('My Sheet')
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
headings = ['Name', 'Surname', 'Age']
for i,heading in enumerate(headings):
ws.write(0, i, heading)
ws.write(1, i, str(dict1[heading]))
wb.save('example.xls')
add a comment |
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
You can save it in excel by saving it as pandas data frame in 2 different ways
Long Format
Converting dictionary to pandas dataframe
df=pd.DataFrame(list(dict1.items())) #Python 3
df=pd.DataFrame(dict1.items()) #Python 2
this will store your data as :
0 1
0 Name Bob
1 Surname red
2 Age 17
Wide Format
Converting dictionary key as columns in pandas
df=pd.DataFrame(dict1, index=[0])
This will convert your data in the desired format
Age Name Surname
0 17 Bob red
And finally, you can save your data in excel
df.to_csv('myfile.csv', index =False)
If it was helpful, then please accept and upvote the answer. Thanks
– Ankur Gulati
Nov 21 '18 at 19:15
add a comment |
Your Answer
StackExchange.ifUsing("editor", function () {
StackExchange.using("externalEditor", function () {
StackExchange.using("snippets", function () {
StackExchange.snippets.init();
});
});
}, "code-snippets");
StackExchange.ready(function() {
var channelOptions = {
tags: "".split(" "),
id: "1"
};
initTagRenderer("".split(" "), "".split(" "), channelOptions);
StackExchange.using("externalEditor", function() {
// Have to fire editor after snippets, if snippets enabled
if (StackExchange.settings.snippets.snippetsEnabled) {
StackExchange.using("snippets", function() {
createEditor();
});
}
else {
createEditor();
}
});
function createEditor() {
StackExchange.prepareEditor({
heartbeatType: 'answer',
autoActivateHeartbeat: false,
convertImagesToLinks: true,
noModals: true,
showLowRepImageUploadWarning: true,
reputationToPostImages: 10,
bindNavPrevention: true,
postfix: "",
imageUploader: {
brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
allowUrls: true
},
onDemand: true,
discardSelector: ".discard-answer"
,immediatelyShowMarkdownHelp:true
});
}
});
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418622%2fpython-copy-dictionary-into-excel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
3 Answers
3
active
oldest
votes
3 Answers
3
active
oldest
votes
active
oldest
votes
active
oldest
votes
import pandas as pd
pd.DataFrame(dict1).to_excel('myfile.xslx')
add a comment |
import pandas as pd
pd.DataFrame(dict1).to_excel('myfile.xslx')
add a comment |
import pandas as pd
pd.DataFrame(dict1).to_excel('myfile.xslx')
import pandas as pd
pd.DataFrame(dict1).to_excel('myfile.xslx')
edited Nov 23 '18 at 2:03
answered Nov 21 '18 at 18:43
ConnerConner
23.4k84568
23.4k84568
add a comment |
add a comment |
If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:
import xlwt
from datetime import datetime
wb = xlwt.Workbook()
ws = wb.add_sheet('My Sheet')
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
headings = ['Name', 'Surname', 'Age']
for i,heading in enumerate(headings):
ws.write(0, i, heading)
ws.write(1, i, str(dict1[heading]))
wb.save('example.xls')
add a comment |
If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:
import xlwt
from datetime import datetime
wb = xlwt.Workbook()
ws = wb.add_sheet('My Sheet')
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
headings = ['Name', 'Surname', 'Age']
for i,heading in enumerate(headings):
ws.write(0, i, heading)
ws.write(1, i, str(dict1[heading]))
wb.save('example.xls')
add a comment |
If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:
import xlwt
from datetime import datetime
wb = xlwt.Workbook()
ws = wb.add_sheet('My Sheet')
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
headings = ['Name', 'Surname', 'Age']
for i,heading in enumerate(headings):
ws.write(0, i, heading)
ws.write(1, i, str(dict1[heading]))
wb.save('example.xls')
If you want to guarantee the heading order (for versions of Python before 3.7), you'll need to specify it in a list. You can use the xlwt library:
import xlwt
from datetime import datetime
wb = xlwt.Workbook()
ws = wb.add_sheet('My Sheet')
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
headings = ['Name', 'Surname', 'Age']
for i,heading in enumerate(headings):
ws.write(0, i, heading)
ws.write(1, i, str(dict1[heading]))
wb.save('example.xls')
answered Nov 21 '18 at 18:53
xnxxnx
15.5k43773
15.5k43773
add a comment |
add a comment |
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
You can save it in excel by saving it as pandas data frame in 2 different ways
Long Format
Converting dictionary to pandas dataframe
df=pd.DataFrame(list(dict1.items())) #Python 3
df=pd.DataFrame(dict1.items()) #Python 2
this will store your data as :
0 1
0 Name Bob
1 Surname red
2 Age 17
Wide Format
Converting dictionary key as columns in pandas
df=pd.DataFrame(dict1, index=[0])
This will convert your data in the desired format
Age Name Surname
0 17 Bob red
And finally, you can save your data in excel
df.to_csv('myfile.csv', index =False)
If it was helpful, then please accept and upvote the answer. Thanks
– Ankur Gulati
Nov 21 '18 at 19:15
add a comment |
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
You can save it in excel by saving it as pandas data frame in 2 different ways
Long Format
Converting dictionary to pandas dataframe
df=pd.DataFrame(list(dict1.items())) #Python 3
df=pd.DataFrame(dict1.items()) #Python 2
this will store your data as :
0 1
0 Name Bob
1 Surname red
2 Age 17
Wide Format
Converting dictionary key as columns in pandas
df=pd.DataFrame(dict1, index=[0])
This will convert your data in the desired format
Age Name Surname
0 17 Bob red
And finally, you can save your data in excel
df.to_csv('myfile.csv', index =False)
If it was helpful, then please accept and upvote the answer. Thanks
– Ankur Gulati
Nov 21 '18 at 19:15
add a comment |
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
You can save it in excel by saving it as pandas data frame in 2 different ways
Long Format
Converting dictionary to pandas dataframe
df=pd.DataFrame(list(dict1.items())) #Python 3
df=pd.DataFrame(dict1.items()) #Python 2
this will store your data as :
0 1
0 Name Bob
1 Surname red
2 Age 17
Wide Format
Converting dictionary key as columns in pandas
df=pd.DataFrame(dict1, index=[0])
This will convert your data in the desired format
Age Name Surname
0 17 Bob red
And finally, you can save your data in excel
df.to_csv('myfile.csv', index =False)
dict1 = {"Name": 'Bob' , "Surname": 'red', "Age": 17 }
You can save it in excel by saving it as pandas data frame in 2 different ways
Long Format
Converting dictionary to pandas dataframe
df=pd.DataFrame(list(dict1.items())) #Python 3
df=pd.DataFrame(dict1.items()) #Python 2
this will store your data as :
0 1
0 Name Bob
1 Surname red
2 Age 17
Wide Format
Converting dictionary key as columns in pandas
df=pd.DataFrame(dict1, index=[0])
This will convert your data in the desired format
Age Name Surname
0 17 Bob red
And finally, you can save your data in excel
df.to_csv('myfile.csv', index =False)
answered Nov 21 '18 at 19:14
Ankur GulatiAnkur Gulati
14010
14010
If it was helpful, then please accept and upvote the answer. Thanks
– Ankur Gulati
Nov 21 '18 at 19:15
add a comment |
If it was helpful, then please accept and upvote the answer. Thanks
– Ankur Gulati
Nov 21 '18 at 19:15
If it was helpful, then please accept and upvote the answer. Thanks
– Ankur Gulati
Nov 21 '18 at 19:15
If it was helpful, then please accept and upvote the answer. Thanks
– Ankur Gulati
Nov 21 '18 at 19:15
add a comment |
Thanks for contributing an answer to Stack Overflow!
- Please be sure to answer the question. Provide details and share your research!
But avoid …
- Asking for help, clarification, or responding to other answers.
- Making statements based on opinion; back them up with references or personal experience.
To learn more, see our tips on writing great answers.
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
StackExchange.ready(
function () {
StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53418622%2fpython-copy-dictionary-into-excel%23new-answer', 'question_page');
}
);
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Sign up or log in
StackExchange.ready(function () {
StackExchange.helpers.onClickDraftSave('#login-link');
});
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Sign up using Google
Sign up using Facebook
Sign up using Email and Password
Post as a guest
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
Required, but never shown
You can import a dictionary to a pandas DataFrame and then write the output to an Excel file, as long as you have OpenPyxl installed. You will need to adjust the structure of your input dict though -- I don't know if this is something that is possible for your use case. Side notes: you wouldn't be able to preserve the order of this dictionary structure you have specified without specifically storing the order separately as a dict is similar to a set (unordered). I would avoid using the Python keyword
dictto name a variable.– Mark
Nov 21 '18 at 18:45