[英]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.