0

I'm trying to export a Python Dataframe to excel using xlsx or csv...

Here is the code I tried to use:

    export_word_count = word_count.to_excel (r'C:\Users\OTR\PycharmProjects\MyProjects\word_count.xlsx', index = None, header=True)

I keep getting the following error messages:

Traceback (most recent call last):
File "C:/Users/OTR/PycharmProjects/MyProjects/CAP_Test_MotsCles.py", line 35, 
in <module>
export_word_count = word_count.to_excel 
(r'C:\Users\OTR\PycharmProjects\MyProjects\word_count_CAP.xlsx', index = None, 
header=True)
File "C:\Users\OTR\PycharmProjects\MyProjects\venv\lib\site- 
packages\pandas\core\generic.py", line 2127, in to_excel
engine=engine)
File "C:\Users\OTR\PycharmProjects\MyProjects\venv\lib\site-packages\pandas\io\formats\excel.py", line 656, in write
writer = ExcelWriter(_stringify_path(writer), engine=engine)
File "C:\Users\OTR\PycharmProjects\MyProjects\venv\lib\site-packages\pandas\io\excel.py", line 1204, in __init__
from openpyxl.workbook import Workbook
ModuleNotFoundError: No module named 'openpyxl'

Any output on this would be greatly appreciated. I tried to tweek the code, but still wouldn't export. Thank you.

EDIT:

Managed to export, but having issues with full data export

Sample Python Data:

products          58
company           53
cannabis          42
business          39
2
  • 2
    pip install openpyxl Commented May 14, 2019 at 19:26
  • There is something wrong with your installation, which seems to be missing the openpyxl library. Try installing it separately. To export it via a csv, use df.to_csv(). Commented May 14, 2019 at 19:32

2 Answers 2

1

You dont have python openpyxl module installed. Install it with:

pip install openpyxl
Sign up to request clarification or add additional context in comments.

5 Comments

I just did, it managed to export. However, my export is a list of most commun words in my dataframe. It only exported the frequency of the words, without the words themselves. So for example, I get : products 58 company 53 ........ but in excel, I get 58 53.
Can you please provide sample of dataframe that you want to export ?
The words themselves are probably your index. So instead remove the argument ‘index = None’.
Klemen, I've provided a sample, but Niels Henkens provided me with the right answer. Thank you all so much for your answers! It did work at last :) It was the index = None that was removing the words.
For those who use the conda distribution (Anaconda or Miniconda), the equivalent is writing in the Anaconda Prompt conda install openpyxl
0

Your words are your index. Right now you are not exporting the index. Try changing your code to:

word_count.to_excel (r'C:\Users\OTR\PycharmProjects\MyProjects\word_count.xlsx', index =True, header=True)

‘index=True’ is the default behavior, so not actually necessary.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.