将pandas数据帧传递到python子进程中.Popen作为参数 [英] passing pandas dataframe into a python subprocess.Popen as an argument

查看:120
本文介绍了将pandas数据帧传递到python子进程中.Popen作为参数的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试从主脚本调用python脚本.我需要从主脚本中仅生成一个数据框,然后将其作为参数传递给子流程脚本,以在子流程中使用.

I am attempting to call a python script from a master script. I need the dataframe to be generated only one from within the master script and then passed on to the subprocess script as an argument to be used inside the subprocess.

以下是我编写所需的python主脚本的尝试.

Following is my attempt at writing the required python master script.

from subprocess import PIPE, Popen
import pandas as pd

test_dataframe = pd.read_excel(r'C:\test_location\file.xlsx',sheetname='Table')

sp = Popen(["python.exe",'C:/capture/test.py'], shell=True, stdin=PIPE, stdout=PIPE, stderr=PIPE)
sp.communicate(test_dataframe)

这是错误: TypeError: argument 1 must be convertible to a buffer, not DataFrame

这是我第一次尝试使用子流程模块,所以我还不是很擅长.任何帮助将不胜感激.

This is my first time trying to use the subprocess module so i am not very good at it yet. Any help will be much appreciated.

推荐答案

子进程启动另一个应用程序.进程之间可能进行通信的方式与python程序中函数进行通信的方式大不相同.您需要通过非pythonic环境传递DataFrame.因此,您需要将其序列化为文本,然后在另一端反序列化.例如,您可以使用pickle模块,然后在一端使用sp.communicate(pickle.dumps(test_dataframe))在另一端使用pickle.loads(sys.stdin.read()).或者,您可以将DataFrame编写为csv,然后再次对其进行解析.或者,您可以使用其他任何格式.

Subprocess launches another application. The ways that processes may communicate between each other significantly differ from ways that functions communicate within python program. You need to pass your DataFrame through a non pythonic environment. So you need to serialize it in-to a text and then deserialize it on other end. For example you can use pickle module and then sp.communicate(pickle.dumps(test_dataframe)) on one end end pickle.loads(sys.stdin.read()) on another. Or you can write your DataFrame as csv and then parse it again. Or you can use any other format.

这篇关于将pandas数据帧传递到python子进程中.Popen作为参数的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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