pandas 将数据从 Python 导出到 Excel

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/46918968/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-14 04:41:22  来源:igfitidea点击:

Export data from Python to Excel

pythonexcelpandasexport

提问by Ara

I'm trying to export data from Python using (http://jupyter.org/) to Excel

我正在尝试使用(http://jupyter.org/)将数据从 Python 导出到 Excel

import pandas as pd
import matplotlib.pyplot as plt
from datetime import datetime
df = pd.read_csv('rr.csv') 
df['COLLISION_DATE'] = pd.to_datetime(df['COLLISION_DATE'],format='%Y%m%d')
df['week'], df['month'], df['year'],df['day'] = df['COLLISION_DATE'].dt.week, df['COLLISION_DATE'].dt.month, df['COLLISION_DATE'].dt.year,df['COLLISION_DATE'].dt.day
df = df.groupby('month').size().to_frame('Number of Accidents') 
df.plot.line()
plt.show()
df
df.to_excel('m.xlsx')

I'm getting error

我收到错误

ModuleNotFoundError: `No module named 'openpyxl'

this is my first project using Python Any Idea whats wrong or any other code that I can Use ?

这是我第一个使用 Python 的项目 Any Idea 有什么问题或我可以使用的任何其他代码?

回答by Sujit

I am using Azure notebook (https://notebooks.azure.com) which is in an online Jupyter notebook. I tried one my DataFrame which I downloaded from Kaggle to export and seems it's working and below is the code.

我正在使用在线 Jupyter 笔记本中的Azure 笔记本 ( https://notebooks.azure.com)。我尝试了一个我从 Kaggle 下载的 DataFrame 导出,似乎它正在工作,下面是代码。

import pandas as pd
df = pd.read_csv('/home/nbuser/armenian_pubs.csv')
df.to_excel('data_set_2.xlsx')

Note that here you need to upload the any data set (CSV) file via Data > Upload menu from local system, then I used the DF to_excel of Panda method to create the Excel with just file name. This creates the file name on the /libraryfolder and from there you can use Data > Download to download the file.

请注意,这里您需要通过本地系统的“数据”>“上传”菜单上传任何数据集(CSV)文件,然后我使用 Panda 方法的 DF to_excel 创建仅具有文件名的 Excel。这会在文件/library夹上创建文件名,您可以从那里使用数据 > 下载来下载文件。

enter image description here

在此处输入图片说明

Hope this will help in your scenario.

希望这对您的场景有所帮助。