如何使用python pandas 从docker容器访问CSV文件(位于pc hdd中)? [英] How to access CSV file (located in pc hdd) from a docker container with python pandas?

查看:128
本文介绍了如何使用python pandas 从docker容器访问CSV文件(位于pc hdd中)?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想实现一种机器学习算法,该算法可以使用 PySEAL 库对同态数据进行操作. PySEAL库作为docker容器发布,带有一个"examples.py"文件,其中显示了一些同态加密示例.我想编辑"examples.py"文件以实现ML算法.我试图以这种方式导入CSV文件-

I want to implement a Machine Learning algorithm which can operate on homomorphic data using PySEAL library. PySEAL library is released as a docker container with an 'examples.py' file which shows some homomorphic encryption example. I want to edit the 'examples.py' file to implement the ML algorithm. I trying to import a CSV file in this way -

dataset = pd.read_csv ('Dataset.csv')

我已成功导入熊猫库.我尝试了很多方法来导入CSV文件,但是失败了.如何导入?

I have imported pandas library successfully. I have tried many approaches to import the CSV file but failed. How can I import it?

我是Docker的新手.详细的过程将非常有帮助.

I am new to Docker. Detailed procedure will be really helpful.

推荐答案

您可以通过Docker构建过程(假设您是创建映像的人)来完成此操作,也可以通过容器在访问过程中可以访问的卷映射来完成此操作.运行时.

You can either do it via the Docker build process (assuming you are the one creating the image) or through a volume mapping that would be accessed by the container during runtime.

要通过构建进行访问,您可以执行Docker Copy命令以在容器的工作空间中获取文件

For access through the build, you could do a Docker Copy command to get the file within the workspace of the container

FROM 3.7

COPY /Dataset.csv /app/Dataset.csv
...

然后,您可以使用pandas.read_csv()函数通过/app/Dataset.csv从容器直接访问文件,例如-

Then you can directly access the file via /app/Dataset.csv from the container using pandas.read_csv() function, like -

data=pandas.read_csv('/app/Dataset.csv')

映射Dataset.csv的卷份额

如果您无法直接控制源图像的创建,或者不希望将数据集与容器打包在一起(根据使用情况,这可能是最佳做法).您可以在启动容器时通过卷映射共享它:

Mapping volume share for Dataset.csv

If you don't have direct control over the source image creation, or do not want the dataset packaged with the container (which may be the best practice depending on the use case). You can share it through a volume mapping when starting the container:

dataset = pd.read_csv ('app/Dataset.csv')

假设您的Dataset.csv位于my/user/dir/Dataset.csv中

通过CLI:

docker run -v my/user/dir:app my-python-container

后一种解决方案的好处是您可以继续在主机上编辑文件"Dataset.csv",该文件将反映您所做的更改,或者将发生python进程.

The benefit of the latter solution is you can then continue to edit the file 'Dataset.csv' on your host and the file will reflect changes made by you OR the python process should that occur.

这篇关于如何使用python pandas 从docker容器访问CSV文件(位于pc hdd中)?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆