简体   繁体   English

每次运行python代码时都必须导入excel文件吗?

[英]Do I have to import excel file every time when I run the python code?

I'm an R user but trying to learn Python.我是 R 用户,但正在尝试学习 Python。 When using R, once I run a single code to import excel file and save it as dataframe, I was able to use dataframe saved in my working space without re-importing every time.使用 R 时,一旦我运行单个代码来导入 excel 文件并将其另存为数据框,我就可以使用保存在工作空间中的数据框,而无需每次都重新导入。

While using Python, I noticed that unless I use interpreter, I only can run full script(whole "foo.py" file) but not code by code.在使用 Python 时,我注意到除非我使用解释器,否则我只能运行完整的脚本(整个“foo.py”文件),而不能逐代码运行。

I would like to load excel file and work with data inside.我想加载 excel 文件并处理里面的数据。 My code begins with importing excel file as dataframe.我的代码从导入 excel 文件作为数据框开始。 Hence, every time I add new code and want to see the result, I am running all py script and it loads the data every time I run it.因此,每次我添加新代码并想查看结果时,我都会运行所有 py 脚本,并且每次运行它时它都会加载数据。

Maybe I am using Python in a wrong way.也许我以错误的方式使用 Python。 With Jupyter notebook I didn't have this issue because I was able to run the code cell by cell just like R. But I am trying to use Pycharm now.使用 Jupyter notebook 我没有这个问题,因为我能够像 R 一样逐个单元地运行代码。但我现在正在尝试使用 Pycharm。

import pandas as pd
df = pd.read_excel('foo.xlsx', sheet_name = 'sales_data')
print("Column headings:")
print(df.columns)

Jupyter which runs a script line by line, and you have the variables including the dataframe which has been loaded to memory, so you can use that df until you exit Jupyter . Jupyter运行脚本,并且您拥有包括已加载到内存的数据帧在内的变量,因此您可以使用该df直到退出Jupyter
Whereas an IDE like PyCharm , depending on the edition, especially Community edition runs the whole script in one go, so it needs to load the excel into memory the next time it runs, because it's not persisting any information from the last run.而像PyCharm这样的 IDE,取决于版本,尤其是社区版,会一次性运行整个脚本,因此下次运行时需要将 excel 加载到内存中,因为它不会保留上次运行的任何信息。

Jupyter is what we call a REPL , which means all user information is persistent till the session is killed, whereas PyCharm runs all the code in one go while it evauates things line by line, and give you the output at the end. Jupyter就是我们所说的REPL ,这意味着所有用户信息都是持久的,直到会话被终止,而PyCharm运行所有代码,同时逐行评估事物,并在最后为您提供输出。

import pandas as pd

def load_file(file_name,sheetname=None):
    df =     pd.read_excel(file_name, sheet_name =sheetname)
    return df

readed_df = load_file('<file-path>',sheetname='<sheet name>')

then on readed_df to perform rest of task that you want to do .然后在readed_df上执行您想做的其余任务。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在Python中导入Excel文件? - How Do I Import an Excel File in Python? 每次在Python中运行代码时,如何以相同的顺序随机化pandas列? - How do I randomize a pandas column in the same order every time I run the code in Python? 如何在每次重新运行程序时停止openpyxl-python清除我的excel文件? - How to stop openpyxl - python from clearing my excel file every time I re-run the program? 每次我用 python 运行代码时,文件地址都会在 vscode 终端中重复 - The file address is reduplicated in the vscode terminal every time I run code with python 虽然我的电脑上已经安装了表格,但每次运行下面的代码时都会出现错误 - Although tables have already been installed on my pc, error appears every time when I run the code below 如何将Excel文件导入python并检查列是否重复? - How do I import excel file to python and check column for duplicates? 我已经编写了 python 代码以将 excel 文件(多张)导入我的 sql 服务器,但是对于大文件,它需要太多时间 - I've written the python code to import excel file (multiple sheets) into my sql server, But for large file it taking too much time 如何修改和运行 my.py 文件中的一大段代码,而不必每次都读取整个.xlsx 文件? - How do I modify and run a chunk of code from my .py file without having to read the whole .xlsx file every time? 每次运行 Jupyter Notebook 时,是否必须使用 fetch_openml 重新下载 MNIST? - Do I have to redownload MNIST with fetch_openml every time I run my Jupyter Notebook? 我在 Visual Studio Code 上使用 Python 来导入 excel 文件,但出现导入错误 - I am using Python on Visual Studio Code to import an excel file but I am getting an import error
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM